Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How to make regular expression allows spaces between words

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to make regular expression allows spaces between words

    Hi, I am learning how to use regex to split a word into syllables and then add a letter p and repetition after each syllable. If a syllable starts with a consonant(s), then the consonant(s) should be removed. In this case, the definition of syllable is zero or more consonants followed by a single vowel followed by zero or more consonants. For example, I have an input:
    Two peas in a pod
    The expected output:
    Twopo pepeaspas inpin apa podpod
    I have written this code, the only problem is that it does not allow spaces between words. May I ask suggestions to adjust the code? Thank you
    import java.util.Scanner;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    // A syllable is defined as: zero or more consonants followed by a single vowel followed by zero or more consonants.
    // In this case, only {aeiou} are considered vowels.
    public class Language {
        public static void main(String[] args) {
            String input = "Two peas in a pod";
            String addP = "p";
            // Test whether the regular expression matches the pattern.
            Matcher m = Pattern.compile("([^aeiou]?[aeiou])((nghhgfdgy|[^aeiou])(?![aeiou]))?", Pattern.CASE_INSENSITIVE).matcher(input);
            int s = 0;
            // Find the next expression that matches the pattern.
            while (m.find()) {
                // If the first letter of syllable starts with a vowel, then add p before repetition
                if (input.charAt(m.start()) == 'a' || input.charAt(m.start()) == 'i' || input.charAt(m.start()) == 'o'
                || input.charAt(m.start()) == 'e' || input.charAt(m.start()) == 'u') {
                    System.out.print(input.substring(s, m.end()).trim() + addP + input.substring(m.start(), m.end()).trim());
                }
                // If the first letter of syllable starts with a consonant, then add p before repetition and remove the first letter of repetition
                else {
                    System.out.print(input.substring(s, m.end()).trim() + addP + input.substring(m.start(), m.end()).substring(1).trim());
                }
            s = m.end();
            }
        }
    }
    Output:
    Twopopepeaspasinpinapapodpod
    Last edited by white canvas; November 1st, 2020 at 12:03 PM. Reason: Adding more info

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: How to make regular expression allows spaces between words

    The code is missing the import statements that are required to compile it. Please post the necessary import statements.

    Why aren't there any comments in the program that describe what it is supposed to do and what the rules are that it is trying to follow?
    Does your instructor penalize you for adding comments to the code?
    Personally I find it very useful when writing code like this that has to follow some rules that those rules are in the code as comments.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make regular expression allows spaces between words

    Thank you for your suggestion. I have edited it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: How to make regular expression allows spaces between words

    Ok, thank you.

    Can you describe what is wrong with the current output? What needs to change for it to be correct?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make regular expression allows spaces between words

    My regular expression cannot allow space between words. When I input a word without spaces, it works well. However, when I have input with spaces, for example:
    Two peas in a pod

    My current output is:
    Twopopepeaspasinpinapapodpod

    But my expected output is (with spaces between words, following the spaces on the input):
    Twopo pepeaspas inpin apa podpod

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: How to make regular expression allows spaces between words

    Ok what needs to be changed to the print statement so that the desired spaces are printed?

    I notice that there are no comments for the regular expression to explain what its different parts are supposed to do.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make regular expression allows spaces between words

    Does it mean that I need to change my print statement rather than changing the regular expression?

    [^aeiou] = Match a single character not present in the list (a,e,i,o,u) - consonant(s)
    [aeiou] = Match a single character present in the list (a,e,i,o,u) - vowel(s)
    (nghhgfdgy|[^aeiou]) = Matches a single consonant or multiple consonants
    (?![aeiou]) = Match a single character present in the list
    Last edited by white canvas; November 1st, 2020 at 12:52 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: How to make regular expression allows spaces between words

    I'm not any good with regular expressions. I never use them.

    Where would be the best place to control when a space was printed? Can a regExp be made to match the desired space so it would be printed as part of the matched String?
    Or can the print statement be changed to print the space where it is desired?

    Why are there calls to the trim() method in the print statement? That would remove spaces from the String being printed.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make regular expression allows spaces between words

    Yes, actually we can handle the spaces with regex, but I have not find the correct pattern to do it.
    Changing the print statement maybe work, I am trying to do it now. When I remove trim(), the output is close to the expected output, but still there is a problem. When I remove trim(), it prints:
    Two po pepeaspas inpin a pa podpod

    The "Two po" and "a pa" are still incorrect

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,071
    Thanks
    53
    Thanked 2,614 Times in 2,564 Posts

    Default Re: How to make regular expression allows spaces between words

    Start with the first thing that is wrong in the output and work on changing the code to fix it. It is better when trying to correct code to minimize the problems and work on them one at a time instead of trying to fix them all at once.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Regular Expression for log analysis
    By ankita shukla in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 20th, 2017, 02:13 PM
  2. Need help with a specific regular expression
    By Cornix in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2014, 09:51 AM
  3. Problem with Regular Expression
    By jdluk87 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2012, 08:44 AM
  4. regular expression issue
    By flamant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 22nd, 2012, 02:57 AM
  5. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM

Tags for this Thread