3

Given the following text:

//[&][$][*]\n81723&8992%9892*2343%8734

I need to get:

1. &
2. $
3. *
4. 81723&8992%9892*2343%8734

The first line defines delimiters that separates the numbers at the second line. There is an undefined number of delimiters.

I made this regex:

//(?:\[([^\]]+)\])+\n(.+)

But only 2 groups are obtained. The first is the last delimiter and the second is the string containing the numbers. I tried but I couldn't get all the delimiters. I'm not good at regex, but I think the first group is being overwritten on every iteration of (?:[([^]]+)])+ and I can't solve this.

Any help?

Regards

Victor

vhtc
  • 790
  • 6
  • 12

4 Answers4

4

That's not a nested group you're dealing with, it's a repeated group. And you're right: when a capturing group is controlled by a quantifier, it gets repopulated on every iteration, so the final value is whatever was captured the last time around.

What you're trying to do isn't possible in any regex flavor I'm familiar with.

Here's a fuller explanation: Repeating a Capturing Group vs. Capturing a Repeated Group

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • 1
    I’ve long had a perverse desire to change Perl regexes so that you still get the last such match in `$1`, `$2` etc as always but on a repeated one you can get all of them in the array `@1`, `@2` etc. For example, with `(?:(foo)(bar))+`. – tchrist Aug 26 '11 at 22:47
  • Sorry, I misunderstood the concept. The link was very helpful! – vhtc Aug 28 '11 at 19:38
  • No need to apologize, it's a very common mistake. The key to learning--whatever the field--is learning the right questions to ask. And that's more of a challenge in the field of regexes than in most other fields; the underlying mechanics are not at all obvious or intuitive. – Alan Moore Aug 28 '11 at 20:50
  • Luckily this is still up after 6 years, but please add context to the link as stated in https://stackoverflow.com/help/how-to-answer – alpipego Nov 05 '17 at 19:24
1

The best thing I see that you could do (with regex) would be something like this:

(?:\[([^\]]+)\])?(?:\[([^\]]+)\])? #....etc....# \n(.+)
Jacob Eggers
  • 9,062
  • 2
  • 25
  • 43
1

You can’t write something like (foo)+ and match against "foofoofoo" and expect to get three groups back. You only get one per open paren. That means you need more groups that you’ve written.

tchrist
  • 78,834
  • 30
  • 123
  • 180
-1

The following regex works for javascript:

(\[.+\])(\[.+\])(\[.+\])\\n(.*)

This assumes your & $ * will have values.

arviman
  • 5,087
  • 41
  • 48
  • 1
    It wasn't me, but I can give you a couple of reasons. 1. you're storing the `[$]` instead of just the `$`. 2. You also expect three delimiters always, when he said, "There is an undefined number of delimiters." – Jacob Eggers Aug 26 '11 at 23:18