user3459805
user3459805

Reputation: 141

Catch all groups of * not preceded by <

I want to catch all groups of * that are not preceded by <

r'(?<!\<)(\*{1,4})'

This only omits <*, but not <** or <***.

I am using Python 3 and the re-library.

Upvotes: 2

Views: 39

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You may use

(?<![<*])(\*{1,4})(?!\*)

See the regex demo

Adding a * to the < in the lookbehind will prevent matching the asterisks when they are also preceded with another asterisk and the negative lookahead will prevent matching the asterisks when they are followed with another asterisk.

Details

  • (?<![<*]) - a negative lookbehind that fails the match if there is a < or * immediately to the left of the current location
  • (\*{1,4}) - 1 to 4 asterisks
  • (?!\*) - a negative lookahead that fails the match if, immediately to the right of the current location, there is an asterisk.

Upvotes: 5

Related Questions