简体   繁体   English

使用正则表达式(python)对括号内的文本进行脱色

[英]Using regex (python) to detext text within (and) brackets

strs = [
    "I like to run head first into a wall (not)"
    "klsjlsk klsjdkls ,m s,mdn,mnsd,m (123)"
    "a b c d e f g h i (j?)"
]

I want to remove the " (not), (123), (j?)" 我要删除“(不是),(123),(j?)”

why is 为什么是

re.sub(r' (\([*]\))$', '', strs(0))

not doing it and what's the proper way? 不这样做,正确的方法是什么?

Consider the following: 考虑以下:

import re


strs = [
    "I like to run head first into a wall (not)",
    "klsjlsk klsjdkls ,m s,mdn,mnsd,m (123)",
    "a b c d e f g h i (j?)"
]

# space(\s)
# openbracket(\()
# anychar(.*)
# smallestpossiblematch(?)
# closebracket(\))
pattern = r'\s*\(.*?\)'

# list comprehension with new strings
new = [re.sub(pattern, '', strs[i]) for i in range(len(strs))]

You're using a capture group (the outside parentheses) to capture everything inside the parentheses. 您正在使用捕获组(外部括号)捕获括号内的所有内容。 However, since you're not reusing that captured data, that's unnecessary. 但是,由于您没有重用捕获的数据,因此没有必要。 If you just want to remove all parentheses and the enclosed text at the end of a line (my guess based on what you supplied), you can do 如果您只想删除行尾的所有括号和随附的文本(我的猜测基于您提供的内容),则可以

re.sub(r'\([^\)]*\)$', '', strs[0])

Example: https://regex101.com/r/FOTTfi/1 示例: https//regex101.com/r/FOTTfi/1

If it's important to remove the space before the parentheses as well, just use a \\s or \\s+ at the start. 如果同样重要的是删除括号前的空格,只需在开头使用\\s\\s+

Yours doesn't work because [*] isn't doing what you think. 您的行不通,因为[*]没有按照您的想法行事。 It's looking for the literal * . 它正在寻找文字* If you want to find any number of characters, use .* instead. 如果要查找任意数量的字符,请改用.*

这是您需要使用print re.sub(r'\\([^)]*\\)', '', strs[0])的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM