1

I currently have the following regex:

P:(?<ParentName>[A-Z,a-z,0-9,-,_]+)-(?<ParentAge>[A-Z,a-z,0-9,-,_]+)
C:(?<Child>[A-Z,a-z,0-9,-,_]+)

which matches with

P:Bob-30
C:Jane

However, I would like be able to express a regex matching with:

P:Jack-35
C:John
C:Marie

I tried this by adding []+ arround the second part:

P:(?<ParentName>[A-Z,a-z,0-9,-,_]+)-(?<ParentAge>[A-Z,a-z,0-9,-,_]+)
[C:(?<Child>[A-Z,a-z,0-9,-,_]+)]+

However this doesn't work and gives an error: 'unmatched parenthesis )'for the last parenthesis.

Anyone an idea how I can express this?

Thank you!

Update The problems seems to be the [] inside []. Working regex for only 1 "C:.."-line:

P:(?<ParentName>[A-Za-z0-9-_]+)-(?<ParentAge>[A-Za-z0-9-_]+)
C:[A-Za-z0-9-_]+\n

Not working regex for multiple "C:.."-lines:

P:(?<ParentName>[A-Za-z0-9-_]+)-(?<ParentAge>[A-Za-z0-9-_]+)
[C:[A-Za-z0-9-_]+\n]+

The first [ in the second line matches with the first ], while it should be the second ].

Gybelle
  • 345
  • 1
  • 3
  • 11
  • JavaScript does not support named groups. – Pointy Apr 19 '19 at 12:33
  • @Pointy ECMAScript 2018 compliant JS regex does. – Wiktor Stribiżew Apr 19 '19 at 12:33
  • @WiktorStribiżew yes I remembered that :) [Here](https://stackoverflow.com/questions/5367369/named-capturing-groups-in-javascript-regex) However it's not widely supported – Pointy Apr 19 '19 at 12:34
  • Thanks! but the named groups work ;). it only gives problems when trying to express them inside a list – Gybelle Apr 19 '19 at 12:35
  • Those commas in your `[ ]` subexpressions seem odd to me; you don't need commas to separate character ranges, and the commas will be taken as part of the allowed character set. – Pointy Apr 19 '19 at 12:37
  • Also the `C:` thing involves `[ ]` inside `[ ]`; what do you intend that to mean? – Pointy Apr 19 '19 at 12:39
  • Woops, you're right about the commas. Updated my question with more details about the []. @Pointy – Gybelle Apr 19 '19 at 12:44
  • Should C:John and C:Marie in the same group as Child like this: https://regex101.com/r/IM0HIn/2 – The fourth bird Apr 19 '19 at 13:17
  • I don't know of any regex syntax that allows nested `[ ]` groups; I can't even imagine what that would mean. A `[ ]` subexpression matches a single character (or not). – Pointy Apr 19 '19 at 13:19

1 Answers1

1

To get C:John and C:Marie in the Child group, you could capture a repeating group inside the child group:

(?<Child>(?:C:[A-Za-z0-9,_]+(?:\s|$))+)
  • (?<Child> Named group
  • (?: Non capturing group
    • C:[A-Za-z0-9,_]+(?:\s|$) Match C: followed by any listed in the character class and then match either a whitespace char or the end of the string
  • )+ Close capturing group and repeat 1+ times
  • ) Close named group

Your pattern might look like:

P:(?<ParentName>[A-Za-z0-9,_]+)-(?<ParentAge>[A-Za-z0-9,_]+)\s*(?<Child>(?:C:[A-Za-z0-9,_]+(?:\s|$))+)

Regex demo

Note that the character class can be updated to [A-Za-z0-9,_]+ where you can list each charcter one time.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70