danbroooks
danbroooks

Reputation: 2800

Regex - returning overlapping matches

How do I change this regular expression:

(`)([^`]+)`|(\*)([^*]+)\*

To give an output like this:

` | hel*low
* | low`orld
* | hello
` | world

When passed:

"`hel*low`orld* *hello* `world`"

http://regex101.com/r/aV5bG0

Upvotes: 0

Views: 87

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89574

You can use this pattern:

(?=([`*])((?>[^`*\W]++|(?!\1)[`*])+)\1)

or this pattern:

(?=([`*])((?:[^`*\W]+|(?!\1)[`*])+)\1)

Upvotes: 2

Related Questions