Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Match substring that doesn't contain specified string literal

Tags:

regex

With the regular expression \*\*([^\*]*)\*\* I can match multiple groups of text inside **'s such as:

this **is** a **test**

Returning is & test.

Given the string that's **right * a test**, how do I adjust my expression to return right * a test? How do I get my expression to exclude two *'s instead of just one?

http://regex101.com/r/aD3pC2

like image 819
danbroooks Avatar asked Apr 13 '26 11:04

danbroooks


2 Answers

You can use the reluctant quantifier .*?:

\*\*.*?\*\*

assuming it is supported by your regex engine.

like image 110
arshajii Avatar answered Apr 15 '26 02:04

arshajii


To avoid all the nasty escaping I will just use this regex:

([*][*])(.*?)\1

And grab matched group #2.

Live Demo: http://www.rubular.com/r/hJY1eXnLty

like image 22
anubhava Avatar answered Apr 15 '26 02:04

anubhava



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!