使用多个触发器分割字符串

时间:2020-02-13 07:20:27

标签: python regex

我必须管理我对语音助手说的所有可能的家庭自动化命令,例如:

  1. “在Corridoio中使用消费,在摄影机中使用 spegni 消费在Salotto中使用 imposta
  2. “照相机中的配件中的镜头,而Corridoio中的 spegni 中的镜头”
  3. 消费在萨拉托的诱惑”

使用单个正则表达式,我必须能够细分每种命令:

  1. (“ corridoio中的 accendi 镜头”,“相机e中的 spegni 镜头”,“ salotto中的 imposta 20”)
  2. (“ 配件在相机和照相机中的位置,”,“ spegni 在走廊中的位置”)
  3. (“ 消费在萨拉托(salotto)中的魅力”)

使用my previous question中的正则表达式修改后的用途,我得到:

>>> print(re.search(r'(accendi.+)(spegni.+)(imposta.+)', "accendi la luce in corridoio, spegni la luce in camera e imposta 20 in salotto").groups())  
('accendi la luce in corridoio, ', 'spegni la luce in camera e ', 'imposta 20 in salotto')

可以,但是不适用于其他命令:

>>> print(re.search(r'(accendi.+)(spegni.+)(imposta.+)', "accendi la luce in camera e cameretta, spegni la luce in corridoio").groups())  
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
AttributeError: 'NoneType' object has no attribute 'groups'  
>>> print(re.search(r'(accendi.+)(spegni.+)(imposta.+)', "accendi la luce in salotto").groups())
Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
AttributeError: 'NoneType' object has no attribute 'groups'  

1 个答案:

答案 0 :(得分:0)

尝试此正则表达式。它不需要所有三个定界符都存在,但是,如果确实存在,则将正确拆分命令:

(accendi(?:(?!spegni).)+)(spegni(?:(?!imposta).)+)?(imposta.+)?

(accendi         #start first group, match accendi
   (?:           #start non-capturing group
      (?!        #start negative lookahead
         spegni  #ensure the next characters aren't spegni
      )          #end negative lookahead
      .          #match any character
   )+            #end non-capturing group, repeat 1 or more times
)                #end first capture group
(spegni          #start second group, match spegni
   (?:           #start non-capturing group
      (?!        #start negative lookahead
         imposta #ensure the next characters aren't imposta
      )          #end negative lookahead
      .          #match any character
   )+            #end non-capturing group, repeat 1 or more times
)?               #end second capture group, make it optional
(imposta         #start third capture group, match imposta
   .+            #match anything 1 or more times
)?               #end third capture group, make optional