1

I need to capture a repeated pattern inside a line.

For instance : toto#titi# or toto#titi#tutu or toto#titi#tutu#tata# etc...

and this is my regex : (?:[\w]*#){1,}

And I need to capture toto, titi, tutu...

But even if Matcher.matches() returns true, the only group I have is the last captured pattern :

toto#titi#-> 1 group titi, toto#titi#tutu -> 1 group tutu, toto#titi#tutu#tata -> 1 group tata.

Could you tell me why and how to solve it?

Many thanks

Adrien

Modzful
  • 177
  • 6

3 Answers3

2

You will want this RegEx: (\w+)#? and go through all matches by

Pattern pattern = Pattern.compile("(\\w+)#?");
Pattern check = Pattern.compile("^[\\w#]+$");
if (!check.matcher(input).matches()) // As requested: Sanity check
    throw new IllegalArgumentException("Bogus input received :(");
Matcher m = pattern.matcher(input);
while (m.find()) {
    String matched = m.group(1); // Iterates over the occurences
    System.out.println("I found " + matched);
}

Output for tata#titi#tutu:

I found tata
I found titi
I found tutu

Not that in such a simple case the code,

for (String matched : input.split("#"))
    System.out.println("I found " + matched);

is essentially equivalent. So you aren't bound to use RegEx here.
By essentially equivalent, I mean that String.split("#") will give you empty Strings from input like #tata#titi##tutu (2 total here), while the regex would require to be altered to (\w*)#? to find those as well.

Community
  • 1
  • 1
AlexR
  • 2,412
  • 16
  • 26
  • groups() are Ok, but I need to have Matcher.matches() to true ! – Modzful Jul 24 '14 at 14:50
  • @Modzful why? Matcher.find() works. If you really need it, you'll need a separate RegEx to check for that: `String.matches("^[\w#]+$")` works here. – AlexR Jul 24 '14 at 14:52
  • Is it not possible to build a single regEx for matches() et groups()? :/ – Modzful Jul 24 '14 at 14:55
  • @Modzful A dynamic number of groups isn't well supported with Java Regex. The provided example is as clean as you can get. – AlexR Jul 24 '14 at 14:55
  • Thank you very much for that. I thought at first that it was about this kind of pitfalls. http://www.regular-expressions.info/captureall.html – Modzful Jul 24 '14 at 15:00
0

Try this code :

   public static void main(String[] args) {
        String s = "toto#titi#tutu#tata#";
        Pattern p = Pattern.compile("(\\w{2})(\\1)(?=#)"); // you need too capture the group.
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }

    }

O/P :

toto
titi
tutu
tata

Note: Returns the same output if string is "jfgjd#toto#titi#tutu#tata#sdas";

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0
 public static void main(String[] args) {
        String s = "jfgjd#toto#titi#tutu#tata#sdas";
        Pattern p = Pattern.compile("(\\w+)(?=#)"); // you need too capture the group.
        Matcher m = p.matcher(s);
        while (m.find()) {
            System.out.println(m.group());
        }

    }

After small modification you get

jfgjd
toto
titi
tutu
tata
slavik
  • 1,223
  • 15
  • 17