I have the following strings:
| Elencia { 1: Simple one, 0: A longer sentence, or maybe not, 2: Another sentence }
| Elencia { 10: Sentence with spaces on left, 0: Same but with some spaces on right }
I would like to catch the name on the left and the number the sentence avoiding the multiple spaces on the left and the right.
I wrote this at the moment:
(?<point>[\d] ):\s (?<content>[\w ] )
but it only catches the part inside the curly brackets and it includes the spaces at the end, which is not what I want.
What I would like to have. First example:
- Elencia as name
- "1" as point, "Simple one" as sentence
- "0" as point, "A longer sentence, or maybe not" as sentence
- "2" as point, "Another sentence" as sentence
Second example:
- Elencia as name
- "10" as point, "Sentence with spaces on left" as sentence
- "0" as point, "Sentence with spaces on left" as sentence
I'm using the regex with gdscript but PHP or JavaScript style would work too.
Thank you for your help!
CodePudding user response:
You may use this regex:
(?:(?<name>\w )\s {|(?!^)\G)\W*(?<point>\d ):\s (?<content>. ?(?=,\s \d :|\s*}))
RegEx Details:
(?:
: Start a non-capture group(?<name>\w )
: Match 1 word characters in group namedname
\s {
: Match 1 whitespace followed by a{
|
: OR(?!^)\G
: Start from end of the previous match
)
: End non-capture group\W*
: Match 0 non-word characters(?<point>\d )
: Match 1 digits in names capture grouppoint
:\s
: Match a:
followed by 1 whitespace(?<content>. ?(?=,\s \d:|\s*})
:(?=
: Start a positive lookahead assertion,\s \d :
: Match comma, 1 whitespace, 1 digits followed by:
|
: OR\s*}
: Match 0 whitespace followed by}
)
: End positive lookahead assertion