I need to find and replace all occurrences of apostrophe character in a string, but only if this apostrophe is not followed by another apostrophe.
That is
abc'def
is a match but
abc''开发者_StackOverflowdef
is NOT a match.
I've already composed a working pattern - (^|[^'])'($|[^'])
but I believe it may be shorter and simpler.
Thanks,
Valery
depends on your environment - if your environment supports lookahead and lookbehind, you can do this: (?<!')'(?!')
Ref: http://www.regular-expressions.info/lookaround.html
I think your pattern is short and precise. You could be using negative lookahead/lookbehind, but they would make it a lot more complex. Maintainability is important.
You'll have to be careful for an uneven number of apostrophes:
abc'''def
where you probably do want to replace the 3rd one and leave the 1st and 2nd in there.
You can do that like this (assuming you already matched string literals and only want to replace the uneven numbered trailing apostrophe):
Search for the pattern:
(('')*)'
and replace it with
$1
which is group 1: the even numbered apostrophes (or no apostrophes at all).
I'm not sure what actual problem you're solving, but in case you're parsing/reading a CSV file, or a string that has the likes of CSV input, I highly recommend using a decent CSV parser. Almost all languages have them in some form or another.
see here nagative lookahed q(?!u)
(?=pattern)
is a positive look-ahead assertion(?!pattern)
is a negative look-ahead assertion(?<=pattern)
is a positive look-behind assertion(?<!pattern)
is a negative look-behind assertion
精彩评论