unj2
unj2

Reputation: 53481

How to use or emulate the lookbehind assertion in C++?

I am looking to perform a search where a string(bar) is not preceeded immediately by any of the two strings (foo) or (boo)

for example

foo=bar should not match

foo=baz bar should match

bar should match

boo=bar should not match

I think the look behind regex would have been perfect for this. However I get an unhandled exception when i try to use (?<! from MSVC++ for the look behind

Is the assertion supported in the ? Is it syntactically different? Or can somebody please help me write this regex or emulate the behavior?

Upvotes: 3

Views: 2031

Answers (2)

John Dvorak
John Dvorak

Reputation: 27277

Not many regex flavors support unlimited lookbehind, but all of them support unlimited lookahead.

You can reverse the string, use a lookahead and reverse again. However, this is expensive for large strings.

You may be able to use bounded lookbehind. Some regex flavors support lookbehind if they know its exact or maximum length.

You may find the entire string without the lookbehind, then discard some matches manually. If overlap is not a concern, you can change the lookbehind into an optional capturing group, then discard the matches where the capturing group did match. When replacing a positive lookbehind, the optional capturing group needs not be optional nor capturing (nor group).

Upvotes: 1

moshbear
moshbear

Reputation: 3322

Look into using Boost.Regex or Boost.Xpressive. MSVC++ 10's regex implementation isn't complete, whereas Boost's (for both Regex and Xpressive) are. Both have their advantages and disadvantages.

Whereas Xpressive is faster due to compile-time regex compilation and provides better syntax checking of regexes, it tends to create a lot of symbols, so you're looking at a few megabytes of increased debugging information, minimum.

Regex is fast and doesn't require expressive use of operators for regex construction. However, it does require linking in a library.

There exist other C++ regex engines that can do perl regexes, but using Boost is very painless and I haven't had any issues with it, no matter what I throw at it.

Upvotes: 2

Related Questions