1

Doing text processing in Python, trying to match <brackets> in the output. The inside can contain any character, but if there are numbers the brackets shall not match if there is more than 1 single digit number. For instance...

< 1? >
<12>
<hot 198663 , ? ... @ key \n 6>

...shall↑match, while...

<0 0>
<9653, 8 test 6>
<18str 500 ing 4. 3 – 6>

...shall↑not.

I have tried with something like <(?:\d{1}|\d{2,}|[^\d])>, but it doesn’t really do it.

1 Answer 1

2

You may use a general pattern like <[^>]*> and restrict it with a (?!(?:[^>]*\b\d\b){2}) negative lookahead:

r'<(?!(?:[^>]*\b\d\b){2})[^>]*>'

See the regex demo

Details

  • < - a <
  • (?!(?:[^>]*\b\d\b){2}) - a negative lookahead that fails the match if exactly 2 occurrences of the following sequence is matched immediately to the right of the current location:
    • [^>]* - any 0+ chars other than >
    • \b\d\b - a 1-digit "whole word"
  • [^>]* - any 0+ chars other than >
  • > - a >
Sign up to request clarification or add additional context in comments.

1 Comment

Класс, спасибо огромное!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.