fneu
fneu

Reputation: 13

regular expression: match everything but single forward- and double backslashes

I'm trying to match a string of arbitrary length with a regular expression (in python), that does not contain a single forward- or double backward slashes.

"[^/]*" works just fine for the forward slash, but "[^/\\\\\\\\]" does obviously not work in a set.

How can I match everything but two consecutive backslashes?

Upvotes: 1

Views: 194

Answers (1)

John Kugelman
John Kugelman

Reputation: 361625

The easiest way is to use negative lookahead to exclude the two patterns.

re.match(r'(?!.*/|.*\\\\)', string)

Upvotes: 1

Related Questions