Linus Oleander
Linus Oleander

Reputation: 18127

Match "ABC" from *ABC*, but not from **ABC**

I'm trying to match ABC from *ABC*, but not from **ABC**.

I've this so far;

/[^\*]{1}\*([^\*]+)\*[^\*]+/

The strings to match can be any of the following.

ABC **Don't match this** DEF
ABC *Match this* DEF
*Match this*
**Don't match this**

Upvotes: 1

Views: 145

Answers (3)

user557597
user557597

Reputation:

Your on the right track.
Without assertions: /(?:^|[^*])\*[^*]+*(?:[^*]|$)/
(use aioobe's regex with assertions, or any combination with/without).

Upvotes: 1

David
David

Reputation: 6084

if the string starts and ends with "*" you could use this:

/^*([^*]+)*$/

Upvotes: 0

aioobe
aioobe

Reputation: 421040

You could use negative look-behind, negative look-ahead. That is

(?<!\*)\*ABC\*(?!\*)

Which reads out as

*ABC* not preceeded by *, and not succeeded by *.

Upvotes: 6

Related Questions