Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex negative lookbehind is doing a normal lookbehind or gives an error

Tags:

java

regex

I'm trying to get regex to capture some data with a negative lookbehind so it won't match if a certain string preceeds it. I know there are two basic formats but neither are working. I'm doing this in a search app and can't use java to augment so the solution has to be purely with regex.

This format gives me an error saying "Regular Expression syntax-error: invalid quantifier"

(?<!Product) Type : (.*?)<

This format acts a normal lookbehind and captures only when Type is preceded by Product:

(?!=Product) Type : (.*?)<

What am I doing wrong?

like image 905
Travis Crum Avatar asked Oct 04 '12 16:10

Travis Crum


People also ask

What is negative Lookbehind regex?

In negative lookbehind the regex engine first finds a match for an item after that it traces back and tries to match a given item which is just before the main match. In case of a successful traceback match the match is a failure, otherwise it is a success.

Can I use negative Lookbehind?

The positive lookbehind ( (? <= ) ) and negative lookbehind ( (? <! ) ) zero-width assertions in JavaScript regular expressions can be used to ensure a pattern is preceded by another pattern.

What is Lookbehind in regex?

Introduction to the JavaScript regex lookbehind In regular expressions, a lookbehind matches an element if there is another specific element before it. A lookbehind has the following syntax: (?<=Y)X. In this syntax, the pattern match X if there is Y before it.

Does Python support negative Lookbehind?

The (? <! \$) is a negative lookbehind that does not match the $ sign. The \d+ matches a number with one or more digits.


1 Answers

(?<!Product)[ ]Type[ ]:[ ](.*?)<

This should do what you want. You have to wrap the spaces in brackets []

It will not match:

Product Type : xyz<

but it will match and capture xyz:

Other Type : xyz<

like image 193
danseery Avatar answered Nov 01 '22 13:11

danseery