-1

I am having trouble in using and decoding Strings Regular Expression.

Here is a pattern, "(?i)((^[aeiou])|(\\s+[aeiou]))\\w+[aeiou]\\b", and is to matched to "Arline ate eight apples and one orange while Anita hadn't any". Unable to identify the result, as to how.

the question is from Thinking in Java by Bruce Eckel, Strings Chapter.

Kindly enlighten.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
ToM
  • 39
  • 4

2 Answers2

-1

?i makes it case-insensitive. ((^[aeiou])|(\s+[aeiou])) means to start a capture group that starts with a vowel [aeiou] or a vowel that starts with any whitespace character \s. Then, one or more whitespace characters \w+, that finally ends with a vowel [aeiou]

Joel Hager
  • 1,361
  • 1
  • 8
  • 14
-2

Explaination:

  • (?i) -- means case-insensitive( its allow a-zA-Z)
  • ^[aeiou] -- means start with aeiou
  • | - uses for or
  • \\s+[aeiou] -- means match one or more space then [aeiou]
  • \\w+ -- means match one or more word character(a-zA-Z0-9_)
  • \\b -- means end with word boundary

Demo here to test match.

Java Code:

String text = "Arline ate eight apples and one orange while Anita hadn't any";
        Pattern pattern = Pattern.compile("(?i)((^[aeiou])|(\\s+[aeiou]))\\w+[aeiou]\\b");
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }

Output:

Arline
 ate
 one
 orange
 Anita
GolamMazid Sajib
  • 6,630
  • 4
  • 17
  • 32