我只有一个|和()一样:
(你好!你好)我叫(鲍勃·罗伯特)
我想提供与regexp匹配的字符串的完整列表:
你好,我叫鲍勃 你好,我叫罗伯特 嗨,我叫鲍勃 嗨,我叫罗伯特
是已经执行此操作的工具(库)吗?
我的第一个问题是将regexp字符串拆分为如下数组:
[['Hello','Hi'],'my name is' ,['Bob','Robert']]
答案 0 :(得分:2)
尝试exrex,认为它应该对您有用
简单脚本
import exrex
print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))
输出
→ python new_test.py
['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my
name is Robert']
答案 1 :(得分:0)
使用正则表达式:-)
re.split(r"(\(.+?\|.+?\))",s)
Out: ['', '(Hello|Hi)', ' my name is ', '(Bob|Robert)', '']
# and for each string in the list:
re.split(r"\((.+?)\|(.+?)\)",'(Hello|Hi)')
Out: ['', 'Hello', 'Hi', '']
答案 2 :(得分:0)
您可以尝试以下解决方案,这里我没有导入任何模块。唯一使用的功能是strip, split and replace
input_string = "(Hello|Hi) my name is (Bob|Robert)"
split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
print ([i.strip().split("|") for i in split_string])
#Output --> [['Hello', 'Hi'], ['my name is'], ['Bob', 'Robert']]
我希望这会有所帮助!
如果您需要查询的最终解决方案,请使用以下代码:
from itertools import product
input_string = "(Hello|Hi) my name is (Bob|Robert)"
split_string = (input_string.replace("("," ").replace(")"," ")).split(" ")
jj = [i.strip().split("|") for i in split_string]
kk = list(product(*jj))
print ([" ".join(i) for i in kk])
#output --> ['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my name is Robert']
上面的代码也适用于:input_string =“(Hello | Hi | Hey)我(name | naam)是(Bob | Robert)”