问题
I have strings such as:
this is a text ( with parts in brackets ) . This is another string ( with a very long string between brackets that should not be removed because it is too long being over 100 characters )
Desired output:
this is a text . This is another string ( with a very long string between brackets that should not be removed because it is too long being over 100 characters )
I can match the bracket content with (with the goal to replace it with an empty string to remove it).
\s\(.+\)\s
Now, if there is no closing bracket, the regex deletes to much text. I would like to delete content between two brackets, but only if the length is < 100 chars. How an I do this with regex? I understand I would need a lookahead expression? I appreciate the help!
Edit: Using the following expression, as suggested doesn't work as solution:
\s\(.+\){1,100}\s
回答1:
Use
\s\([^()]{0,100}\)\s
See proof. Set the limiting quantifier after the [^()]
pattern, it matches any character other than parens.
Example code:
import re
test_str = "this is a text ( with parts in brackets ) . This is another string ( with a very long string between brackets that should not be removed because it is too long being over 100 characters )"
print( re.sub(r"\s\([^()]{0,100}\)\s", "", test_str) )
Output:
this is a text. This is another string ( with a very long string between brackets that should not be removed because it is too long being over 100 characters )
来源:https://stackoverflow.com/questions/62427688/regex-delete-between-brackets-but-only-if-below-character-length