Joey Hipolito
Joey Hipolito

Reputation: 3166

Regex capture group with back references

So I am trying to parse some text file with fairly repetitive patterns, and regex will just do the job just fine. But stumbled upon this scenario which looks like so:

2 people:
Juan
Gabriella

I wanted to group Juan and Gabriella so that the result of my Regexp looks like so:

Match 0: 2 people Group 1: Juan Group 2: Gabriella

I tried:

/^\d+\speople.*:$\n(.*)$\n/gm

And the result is:

Match 0: 2 people
Group 1: Juan

I think we can use back references, but unsure how it can be used in this scenario.

Regexr: https://regexr.com/3k86r

Update:

As the comments states, that it is unlikely to do it that way, so how about having Juan and Gabriella in the same group, and just split them afterward.

So the regex will now look for 3 consecutive line breaks to group the items Juan\nGabriella and Foo\nBar\Bazz

2 people: Juan Gabriella

3 people: Foo Bar Bazz

Tried:

\d+\speople+:$([\s\S]*(?=\n{3,}))

https://regexr.com/3k888

Upvotes: 2

Views: 72

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

So the regex will now look for 3 consecutive line breaks to group the items Juan\nGabriella and Foo\nBar\Bazz

You may use

/(?:^|\n)\d+\s*people:([\s\S]*?)(?=\n{3}|$)/

See the regex demo

Details

  • (?:^|\n) - start of string or LF
  • \d+ - 1+ digits
  • \s* - 0+ whitespace chars
  • people: - a literal substring
  • ([\s\S]*?) - Group 1 capturing any 0+ chars as few as possible before the first...
  • (?=\n{3}|$) - 3 consecutive LF symbols or end of string.

JS demo:

var rx = /(?:^|\n)\d+\s*people:([\s\S]*?)(?=\n{3}|$)/g;
var str = "2 people:\nJuan\nGabriella\n\n\n3 people:\nFoo\nBar\nBazz";
let m, res=[];
while (m=rx.exec(str)) {
  console.log(m[1].trim().split("\n"));
}

Upvotes: 1

Related Questions