Regex: delete between brackets, but only if below character length

谁说胖子不能爱 提交于 2020-07-03 10:05:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!