Sid
Sid

Reputation: 234

Perl/Javascript Regular Expression with an 'and' operator

I need to match incorrect backslashes in a text. The following text is an example:

\.br\ Random Words \.br\\1 Testing\.br\2\ Check

So the \.br\ are correct, however the backslashes in \1 and 2\ are not.

So I attempted a regular expression to match any \ which is not followed by a .br but that failed because it would match the closing \ in \.br\

I then looked up a few similar questions on stackoverflow and most of them stated that a series of lookaheads can be used as an 'and' operator and so I tried this:

/(?!\\\.br)\\(?!\.br\\)/

What I attempted to do, was match any backslash that was neither precedeed by a \.br nor followed by a .br\ but it didn't seem to work.

Any help would be appreciated. I hope I haven't missed out any details in the question.

Thanks,

Sid

Upvotes: 0

Views: 80

Answers (3)

Jerry
Jerry

Reputation: 71578

I would use perl, and with a \G anchor and a \K meta character (and some atomic/possessive parts to improve efficiency):

\G(?>\\\.br\\|[^\\]++)*+\K\\

It should be faster than using lookarounds, since there's no duplication of matches (going over the same substring more than once, which is what lookarounds do).

regex101 demo.

Matches completed with 24 and 21 steps respectively (as opposed to using lookarounds using 36 and 22 steps, plus 4 failing steps).

Upvotes: 2

celerno
celerno

Reputation: 1387

(?:\\(?!\.br)\\)+(\S+)

The regex above will capture those characters inside backslashes that are not .br.

*Please note that the number 2 in \.br\2\ will not be captured as .br\ is correctly typed.

Upvotes: 0

ikegami
ikegami

Reputation: 386386

Close. (?!PAT) means "not followed by PAT". You want "not preceded by PAT".

(?<!\\\.br)\\(?!\.br\\)

The following will be a bit faster:

\\(?<!\\\.br\\)(?!\.br\\)

Upvotes: 6

Related Questions