Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx lookahead on .*

Tags:

regex

pcre

I have a pattern that needs to find the last occurrence of string1 unless string2 is found anywhere in the subject, then it needs the first occurrence of string1. In order to solve this I wrote this inefficient negative lookahead.

/(.(?!.*?string2))*string1/

It takes several seconds to run (prohibitively long on subjects lacking any occurrence of either string). Is there a more efficient way to accomplish this?

like image 613
Umbrella Avatar asked Jun 18 '13 19:06

Umbrella


People also ask

What is a lookahead in regex?

Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions.

What is regex positive lookahead?

The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign. You can use any regular expression inside the lookahead (but not lookbehind, as explained below). Any valid regular expression can be used inside the lookahead.

Can I use negative lookahead?

Negative lookahead That's a number \d+ , NOT followed by € . For that, a negative lookahead can be applied. The syntax is: X(?! Y) , it means "search X , but only if not followed by Y ".

What is positive and negative lookahead?

Positive lookahead: (?= «pattern») matches if pattern matches what comes after the current location in the input string. Negative lookahead: (?! «pattern») matches if pattern does not match what comes after the current location in the input string.


1 Answers

You should be able to use the following:

/string1(?!.*?string2)/

This will match string1 as long as string2 is not found later in the string, which I think meets your requirements.

Edit: After seeing your update, try the following:

/.*?string1(?=.*?string2)|.*string1/
like image 170
Andrew Clark Avatar answered Sep 21 '22 17:09

Andrew Clark