9

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

2
  • like this?? rubular.com/r/5oHGVS3r1c
    – xkeshav
    May 20, 2011 at 10:03
  • What environment is this? Perl? Javascript? PHP? Java? POSIX? Not all regex syntaxes are the same.
    – Lukas Eder
    May 20, 2011 at 10:03

4 Answers 4

14

depends on your environment - if your environment supports lookahead and lookbehind, you can do this: (?<!')'(?!')

Ref: http://www.regular-expressions.info/lookaround.html

0
2

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.

3
  • ye, I've been using regexp for years and only took the time to understand lookahead and lookbehind yesterday, because of a stackoverflow question. Probably says more about me than the feature, but bottom line is that the original version is clear even when you have a more basic knowledge of regexp, and is consistent across all environments.
    – Tao
    May 20, 2011 at 10:10
  • @Tao: That is exactly what i am talking about :)
    – jwueller
    May 20, 2011 at 10:10
  • Totally agree, while lookahead/lookbehind example above is nice, short and correct, my own primitive version is easier to understand for non regex savvy developers. Still in doubt which approach to use :)
    – ValeryC
    May 20, 2011 at 12:24
2

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.

1
  • No, this is not CSV. Actually I was solving an issue with Java class MessageFormat that swallows single apostrophe character.
    – ValeryC
    May 20, 2011 at 13:06
0

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

http://www.regular-expressions.info/lookaround.html

working DEMO

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.