atoms
atoms

Reputation: 3093

How to repeat a regex group until end of string

I have the following string someaddres.com/?f=[B]a-test,a test,Test[C]test a,test2

I'm trying to pull two groups from it:

[B]a-test,a test,Test

and

[C]test a,test2

How would I repeat the capture group until a character not present in the group is found?

My current regex is: f=(\[[A-Z]\][a-zA-Z0-9,-\s]+)

Upvotes: 2

Views: 1140

Answers (1)

anubhava
anubhava

Reputation: 785088

You may use this regex with a captured group that will match twice:

(\[\w+\][^[]+)

RegEx Demo

If you want 2 capture groups in single match then use:

(\[\w+\][^[]+)(\[\w+\][^[]+)

RegEx Demo 2

RegEx Details:

  • (: Start capture group #1
    • \[: Match a [
    • \w+: Match 1+ word characters
    • \]: Match a ]
    • [^[]+: Match 1+ of any characters that is not [`
  • ): End capture group #1

Upvotes: 1

Related Questions