Robert N. Dean
Robert N. Dean

Reputation: 1309

Skip quotes in regex

I have the following regex

val=\"(?<val>.*?)\"

it works ok for val="value"

Now I need regex that will match val="value" and val=value

Could you please help? I don't understand how to build such regex. I have tried the following but no success

val=[^"](?<val>.*?)[^"]

update

it seems works val=(?:[^"])*(?<val>.*?)(?:[^"]|")* but I'm not sure that it is correct

Upvotes: 1

Views: 76

Answers (1)

tripleee
tripleee

Reputation: 189297

You can capture the optional opening quote, and require it to be present at the end of the match.

val=(\"?)(?<val>.*?)\1

The back-reference \1 recalls the text which matched the first parenthesized expression.

Obviously, if you have code which depends on the order of grouped parentheses, you need to refer to the second group to get val; but of course you are likely referring to it by name already (otherwise why use a named group?)

The expression [^"] matches a character which isn't a quote, so it's completely wrong here.

Of course, when there aren't any quotes, the expression .*? will match the empty string if there isn't a trailing context which forces it to match something longer. Perhaps you can use something like

val=(\"?)(?<val>.*?)\1(\s|$)

but this will obviously depend on what exactly you are hoping to match and in what context. If not this then maybe you can constrain the value so that you can use a greedy match instead? For instance,

val=(\"?)(?<val>[^\"]*)\1

Upvotes: 1

Related Questions