简体   繁体   English

如何将正则表达式转换为列表

[英]How to transform regexp to list

I've got a regexp string with only | 我只有一个| and () like : 和()像:

(Hello|Hi) my name is (Bob|Robert) (你好|你好)我叫(鲍勃|罗伯特)

And I would like to have the complete list of string who match the regexp : 而且我想获得与regexp匹配的字符串的完整列表:

Hello my name is Bob Hello my name is Robert Hi my name is Bob Hi my name is Robert 你好,我叫鲍勃,你好,我叫罗伯特,嗨,我叫鲍伯,嗨,我的名字,罗伯特

Is it a tool (librairy) who already do this ? 是已经执行此操作的工具(图书馆)吗?

My first problem is to split the regexp string into a array of array like : 我的第一个问题是将regexp字符串拆分为如下数组:

[['Hello','Hi'],'my name is' ,['Bob','Robert']]

Try exrex, think that should work for you 尝试exrex,认为这对您有用

Simple script 简单的脚本

import exrex
print(list(exrex.generate('(Hello|Hi) my name is (Bob|Robert)')))

Output 产量

→ python new_test.py
['Hello my name is Bob', 'Hello my name is Robert', 'Hi my name is Bob', 'Hi my 
name is Robert']

https://github.com/asciimoo/exrex https://github.com/asciimoo/exrex

Do it with regex:-) 用正则表达式来做:-)

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', '']

You can try below solution, here I haven't imported any module. 您可以尝试以下解决方案,这里我还没有导入任何模块。 The only functions used are strip, split and replace 唯一使用的功能是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']]

I hope this helps! 我希望这有帮助!


If you need the final solution to your query then use below code: 如果您需要查询的最终解决方案,请使用以下代码:

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']

The above code will also work for: input_string = "(Hello|Hi|Hey) my (name|naam) is (Bob|Robert)" 上面的代码也适用于:input_string =“(Hello | Hi | Hey)我的(name | naam)是(Bob | Robert)”

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

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