Reputation: 9605
I'm using the regex
(?:^|;)\s*([^=]*[^=\s])\s*=\s*([^;]*[^;\s])
on the following string
"""A"" = .B; ""C"" = .D; ""E"" = .F"
The second capture group ([^;]*[^;\s])
matches the text .B
, .D
and .F"
, whilst the first capture group matches the text """A""
, "C""
and ""E""
.
How can I update this regex to match the text only, i.e., .B
, .D
and .F
, and A
, C
and E
?
I've tried add the quoted to the capture groups, e.g., ([^=\"]*[^=\s])
, but this seems to have no affect.
Upvotes: 2
Views: 703
Reputation: 627488
You may match zero or more quotes before the key value and then restrict the [^=\s]
character class to avoid matching "
by adding it to the class and again match 0+ quotes right after:
(?:^|;)\s*"*([^=]*[^=\s"])"*\s*=\s*([^;]*[^;\s"])
^^ ^ ^^ ^
See the regex demo. Note that [^;]*
will also match double quotes if any since it is a greedy pattern.
Details
(?:^|;)
- start of string or ;
\s*
- 0+ whitespaces"*
- 0+ double quotes([^=]*[^=\s"])
- Group 1:
[^=]*
- 0+ chars other than =
[^=\s"]
- a char other than =
, whitespace and "
"*
- 0+ double quotes\s*=\s*
- a =
enclosed with 0+ whitespaces([^;]*[^;\s"])
- Group 2:
[^;]*
- 0+ chars other than ;
[^;\s"]
- a char other than ;
, whitespace and "
.Upvotes: 4