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?
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.
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.
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.
The (? <! \$) is a negative lookbehind that does not match the $ sign. The \d+ matches a number with one or more digits.
(?<!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<
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With