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''def
is NOT a match.
I've already composed a working pattern - (^|[^'])'($|[^'])
but I believe it may be shorter and simpler.
Thanks,
Valery
Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.
The character + in a regular expression means "match the preceding character one or more times". For example A+ matches one or more of character A. The plus character, used in a regular expression, is called a Kleene plus .
The \ is known as the escape code, which restore the original literal meaning of the following character. Similarly, * , + , ? (occurrence indicators), ^ , $ (position anchors) have special meaning in regex. You need to use an escape code to match with these characters.
A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With