Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match everything up to a specific character whether it exists or not

Tags:

regex

How do I match everything on the line but stop when I find a semicolon? If I never find one I should continue matching even if one doesn't exist.

Here is some sample input:

captureme; dontcaptureme
captureme andme andme
captureme andme andme; butnotme

I've tried a lazy quantifier (\w*?); but it won't work if I make the semicolon optional. See my current regex fiddle here.

like image 940
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Dec 21 '25 21:12

ᴘᴀɴᴀʏɪᴏᴛɪs


1 Answers

You can use

^[^;\n]*

With multiline modifier.

See demo

The ^ in a multiline mode will match at the beginning of a line, and the negated character class [^;\n] will match any character but a semi-colon or a newline symbol. * will make the regex engine match those characters 0 or more times. Thus, if you do not want to match emoty lines, use the + quantifier instead.

like image 100
Wiktor Stribiżew Avatar answered Dec 23 '25 11:12

Wiktor Stribiżew



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!