gordom
gordom

Reputation: 23

RegEx to find all occurrence of some character with exclusion of certain part of text

I’m trying to write a regex to find all occurrence of ' character but exclude these parts of text that begin with [ or < and end with ] or >.

Here is an exemplary text:

This is some text with ' character to find and [text to 'exclude' with letters and numbers 0-9 '' ] and another part of text with 'more characters' to find <and 'more to' exclude> and again some text with 'character' to find

I tried regex with negative lookahead:

(?!((\[|<)[\s\S]*?(\]|>)))'

but it didn’t work.

After several hours I’m run out of ideas :(. Any help will be appreciated.

Upvotes: 2

Views: 50

Answers (1)

Jan
Jan

Reputation: 43169

You may either use

\[.*?\]|<.*?>|(')

And use the captured groups (see a demo on regex101.com) or use the (*SKIP)(*FAIL) version if your engine (PHP, Perl, PCRE in general, ...) supports it:

(?:\[.*?\]|<.*?>)(*SKIP)(*FAIL)|'

See a demo for the latter here.

Upvotes: 1

Related Questions