简体   繁体   English

从正则表达式创建所有组合

[英]Create all combinations from regex

I have sentences that define a template for random combinations: 我有一些句子定义了随机组合的模板:

I like dogs/cats    
I want to eat today/(the next day)

I tried using a regex: 我尝试使用正则表达式:

m = re.search(r'(?P<list>[A-Za-z]+/([A-Za-z]+)+)', sentence)
words = m.group('list').split('/')
combs = [comb for comb in [sentence.replace(m.group('list'), w) for w in words]]

For the first sentence I get ['i like dogs', 'i like cats'] which is what I want. 对于第一句话,我想要的是['i like dogs', 'i like cats'] For the second sentence, re.search returns None . 对于第二句话, re.search返回None What I would like to get is ['I want to eat today', 'I want to eat the next day'] . 我想要得到的是['I want to eat today', 'I want to eat the next day']

How do I need to change the regex? 我该如何更改正则表达式?

(I want to eat today)*|(the next day) (我今天想吃)* |(第二天)

Is the regex that will select the text you want... 正则表达式将选择您想要的文本...

r'(?P<list>[A-Za-z]+/([a-zA-Z]+|\\(.+?\\)))''

([a-zA-Z]+|\\(.+?\\)) matches strings like "word" or "(some word)". ([a-zA-Z]+|\\(.+?\\))匹配字符串,例如“单词”或“(某些单词)”。 And it also matches "()", we need to remove heading "(" and trailing ")" using strip . 并且它也匹配“()”,我们需要使用strip删除标题“(”和尾随“)”。

m = re.search(r'(?P<list>[A-Za-z]+/([a-zA-Z]+|\(.+?\)))', sentence)
words = m.group('list').split('/')
combs = [comb for comb in [sentence.replace(m.group('list'), w.strip('()')) for w in words]]

With below code you will get something like 使用以下代码,您将获得类似

> sentence = 'I want to eat today/(the next day)' m =
> re.search(r'(?P<list>[A-Za-z]+/([A-Za-z]+|(\(.*?\))))', sentence)
> print m.group('list') words = m.group('list').split('/') combs = [comb
> for comb in [sentence.replace(m.group('list'), w) for w in words]]
> print combs

['I want to eat today', 'I want to eat (the next day)'

you could dome extra processing to get rid of the extra parenthesis which should be easy 您可以进行额外的圆顶处理以消除多余的括号,这应该很容易

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

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