7

I have this RegEx:

('.+')

It has to match character literals like in C. For example, if I have 'a' b 'a' it should match the a's and the ''s around them.

However, it also matches the b also (it should not), probably because it is, strictly speaking, also between ''s.

Here is a screenshot of how it goes wrong (I use this for syntax highlighting):
screenshot

I'm fairly new to regular expressions. How can I tell the regex not to match this?

user513951
  • 12,445
  • 7
  • 65
  • 82

4 Answers4

12

It is being greedy and matching the first apostrophe and the last one and everything in between.

This should match anything that isn't an apostrophe.

('[^']+')

Another alternative is to try non-greedy matches.

('.+?')
gpojd
  • 22,558
  • 8
  • 42
  • 71
  • Thanks, this works. What does "greedy" exactly mean in regular expressions? –  Aug 10 '11 at 17:14
  • Won't work with `'\''`, which is a char literal in C (as the question states, it needs to match them).` – sidyll Aug 10 '11 at 17:15
  • 2
    This page, http://www.regular-expressions.info/repeat.html, can explain better than I can explain. Basically, it will match as much as possible when it is greedy. – gpojd Aug 10 '11 at 17:16
  • 1
    @sidyll perhaps `('([^'\\]|\\.)*')` then? It worked. –  Aug 10 '11 at 17:25
4

Have you tried a non-greedy version, e.g. ('.+?')?

There are usually two modes of matching (or two sets of quantifiers), maximal (greedy) and minimal (non-greedy). The first will result in the longest possible match, the latter in the shortest. You can read about it (although in perl context) in the Perl Cookbook (Section 6.15).

miku
  • 181,842
  • 47
  • 306
  • 310
  • This is good advice for Perl-compatible regexes, but an error (which often simply fails quietly) for other regex dialects. – tripleee Dec 10 '15 at 13:16
1

Try:

('[^']+')

The ^ means include every character except the ones in the square brackets. This way, it won't match 'a' b 'a' because there's a ' in between, so instead it'll give both instances of 'a'

Thariq Shihipar
  • 1,072
  • 1
  • 12
  • 27
  • 2
    The `^` in your example is being used as an anchor. It needs to be inside the brackets to work as you expect. – gpojd Aug 10 '11 at 17:15
-1

You need to escape the qutoes:

\'[^\']+\'

Edit: Hmm, we'll I suppose this answer depends on what lang/system you're using.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • It's ObjC: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html – miku Aug 10 '11 at 17:15