Modzful
Modzful

Reputation: 177

Repeating a Capturing Group vs. Capturing a Repeated Group

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

Upvotes: 1

Views: 174

Answers (3)

AlexR
AlexR

Reputation: 2408

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.

Upvotes: 2

slavik
slavik

Reputation: 1303

 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

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

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";

Upvotes: 0

Related Questions