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

2024-05-18 23:43:46 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  1. accendila luce在corridoio,spegnila luce在camera eImporta20在salotto”
  2. accendila luce in camera e cameretta,spegnila luce in Corridoo”
  3. accendila luce在萨洛托”

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

  1. (“accendila luce in corridoio”,“spegnila luce in camera e”,“Importa20 in salotto”)
  2. (“accendila luce in camera e cameretta,”,“spegnila luce in corridoio”)
  3. (“accendila luce in 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'  

Tags: in命令lacameragroupsprintlucecameretta
1条回答
网友
1楼 · 发布于 2024-05-18 23:43:46

试试这个正则表达式。它不要求所有三个分隔符都存在,但如果它们都存在,则命令将被正确拆分:

(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

相关问题 更多 >

    热门问题