Python 中的正则表达式前瞻和后视多次
Regex Lookahead and lookbehind multiple times in Python
我的输入格式如下 (txt1):
txt1 = "[('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals'), ('a')]"
我要解压成如下格式-
[['1','Hello is 1)people 2)animals'],['People are 1) hello 2) animals'],['a']]
所以,基本上,我想要括号内的信息。但我一直没能做到。此外,我还使用了 Lookahead 和 Lookbehind 来避免按数字拆分 - '1)' 或 '2)',这在我执行 re.split('[\(\)\[\]]
的简单语句时发生了
我一直在尝试先使用 findall 函数来检查我得到了什么。
r = re.findall(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
我一直在-
["('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals')"]
好像忽略了中间的括号。我该怎么做才能获得我需要的结果?
谢谢。
注:
对于我打算用来获得所需输出的拆分函数,我得到了这个-
r = re.split(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
['[', ", ('a')]"]
为什么使用正则表达式?
import ast
[list(x) if isinstance(x, tuple) else [x] for x in ast.literal_eval(txt1)]
# => [['1', 'Hello is 1)people 2)animals'], ['People are 1) hello 2) animals'], ['a']]
如果您坚持使用正则表达式,除非字符串包含转义引号,否则这应该有效:
[re.findall(r"'[^']*'", x) for x in re.findall(r"\(('[^']*'(?:,\s*'[^']*')*)\)", txt1)]
# => [["'1'", "'Hello is 1)people 2)animals'"], ["'People are 1) hello 2) animals'"], ["'a'"]]
无需使用regex
的另一种解决方案:
txt1 = "[('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals'), ('a')]"
replace_pairs = {
"('": "'",
"'), ": '#',
'[': '',
']': '',
"'": '',
}
for k, v in replace_pairs.items():
txt1 = txt1.replace(k, v)
txt1 = txt1[:-1].split('#') # the last char is a paranthesis
print([i.split(',') for i in txt1])
输出:
[['1', 'Hello is 1)people 2)animals'], ['People are 1) hello 2) animals'], ['a']]
注意:如果输入比您在此处显示的更复杂,这可能不起作用。
您可以尝试使用模式 \(([^(]+)\)
解释:
\(
- 按字面意思匹配 (
(...)
- 捕获组
[^(]+
- 匹配 (
以外的一个或多个字符
\)
- 按字面意思匹配 )
并使用替换模式:[]
,将第一个捕获组(反向引用
)放在方括号内。
我的输入格式如下 (txt1):
txt1 = "[('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals'), ('a')]"
我要解压成如下格式-
[['1','Hello is 1)people 2)animals'],['People are 1) hello 2) animals'],['a']]
所以,基本上,我想要括号内的信息。但我一直没能做到。此外,我还使用了 Lookahead 和 Lookbehind 来避免按数字拆分 - '1)' 或 '2)',这在我执行 re.split('[\(\)\[\]]
我一直在尝试先使用 findall 函数来检查我得到了什么。
r = re.findall(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
我一直在-
["('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals')"]
好像忽略了中间的括号。我该怎么做才能获得我需要的结果?
谢谢。
注:
对于我打算用来获得所需输出的拆分函数,我得到了这个-
r = re.split(r'\((?=\').*(?<=\')\)(?=\,)', txt1)
['[', ", ('a')]"]
为什么使用正则表达式?
import ast
[list(x) if isinstance(x, tuple) else [x] for x in ast.literal_eval(txt1)]
# => [['1', 'Hello is 1)people 2)animals'], ['People are 1) hello 2) animals'], ['a']]
如果您坚持使用正则表达式,除非字符串包含转义引号,否则这应该有效:
[re.findall(r"'[^']*'", x) for x in re.findall(r"\(('[^']*'(?:,\s*'[^']*')*)\)", txt1)]
# => [["'1'", "'Hello is 1)people 2)animals'"], ["'People are 1) hello 2) animals'"], ["'a'"]]
无需使用regex
的另一种解决方案:
txt1 = "[('1','Hello is 1)people 2)animals'), ('People are 1) hello 2) animals'), ('a')]"
replace_pairs = {
"('": "'",
"'), ": '#',
'[': '',
']': '',
"'": '',
}
for k, v in replace_pairs.items():
txt1 = txt1.replace(k, v)
txt1 = txt1[:-1].split('#') # the last char is a paranthesis
print([i.split(',') for i in txt1])
输出:
[['1', 'Hello is 1)people 2)animals'], ['People are 1) hello 2) animals'], ['a']]
注意:如果输入比您在此处显示的更复杂,这可能不起作用。
您可以尝试使用模式 \(([^(]+)\)
解释:
\(
- 按字面意思匹配 (
(...)
- 捕获组
[^(]+
- 匹配 (
\)
- 按字面意思匹配 )
并使用替换模式:[]
,将第一个捕获组(反向引用 )放在方括号内。