Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - pattern capture everything except for pattern [.net]

Tags:

.net

regex

I would like to capture anything up to, but not including a particular patter. My actual problem has to do with parsing out information from html, but I am distilling the problem down to an example to, hopefully, clarify my question.

Source

xaxbxcabcabc

Desired Match

xaxbxc

If I use a lookahead the expression will capture the first occurrence

.*(?=abc) => xaxbxcabc

I would like something along the lines of a negated character class, just for a negated pattern.

.*[^abc] //where abc as a pattern instead of a list giving anything but a, b or c

I am using http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx for testing

like image 547
Hypnovirus Avatar asked Apr 27 '26 17:04

Hypnovirus


2 Answers

A non-greedy (lazy) quantifier *? could be useful here, e.g.

^(?<captured>.*?)abc.*$

Edit: Just to be clear, the explicit capture is (of course) not needed, the really important part is just

(.*?)abc
like image 187
Mormegil Avatar answered Apr 29 '26 06:04

Mormegil


If you anchor the regex you'll solve the problem (+ use of lazy quantifier):

"^.*?(?=abc)"
like image 41
xanatos Avatar answered Apr 29 '26 07:04

xanatos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!