Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

regex - How can I match double-quoted strings with escaped double-quote characters?

I need a Perl regular expression to match a string. I'm assuming only double-quoted strings, that a " is a literal quote character and NOT the end of the string, and that a is a literal backslash character and should not escape a quote character. If it's not clear, some examples:

"""    # string is 1 character long, contains dobule quote
""    # string is 1 character long, contains backslash
"""  # string is 2 characters long, contains backslash and double quote
""  # string is 2 characters long, contains two backslashes

I need a regular expression that can recognize all 4 of these possibilities, and all other simple variations on those possibilities, as valid strings. What I have now is:

/".*[^\]"/

But that's not right - it won't match any of those except the first one. Can anyone give me a push in the right direction on how to handle this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

/"(?:[^"]|\.)*"/

This is almost the same as Cal's answer, but has the advantage of matching strings containing escape codes such as .

The ?: characters are there to prevent the contained expression being saved as a backreference, but they can be removed.

NOTE: as pointed out by Louis Semprini, this is limited to 32kb texts due a recursion limit built into Perl's regex engine (that unfortunately silently returns a failure when hit, instead of crashing loudly).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...