簡體   English   中英

匹配所有模式出現的情況,但不要用雙引號引起來

[英]Match all pattern occurences but not in double quotes

我正在嘗試解決下一個任務。 例如,我有這樣的情感:

(1) "Je suis arrivé en Californie il y a quatre décennies 
avec absolument rien et grâce à l'accueil que cet Etat m'a offert, 
j'ai tout obtenu ", a-t-il dit dimanche dans son ultime allocution
hebdomadaire en tant que gouverneur.
(2) En revanche, je ne crois pas que l'on puisse engager durablement des soldats 
dans un conflit long et dur sans un soutien populaire fort.
(3) This is another sample with je and in double quotes "like je here" as well.

我需要一個與句子匹配的正則表達式,如果它包含'Je'或'je'但沒有用雙引號引起來。 現在我正在使用下一個正則表達式:

(.*)\s((^Je|je)|(Je|je))\s(.*) 

但是當模式在雙引號內時,它也匹配情感。 例如,從上面的示例中,只有(2)和(3)句子應與正則表達式匹配。

您可以使用此正則表達式搜索字符串中的模式:

s = 'This is another sample with je and in double quotes "like je here" as well.'    
re.search(r'[jJ]e(?=(?:[^"]*"[^"]*")*[^"]*$)', s)  # Match

s = 'This is another sample with and in double quotes "like je here" as well.'
re.search(r'[jJ]e(?=(?:[^"]*"[^"]*")*[^"]*$)', s)  # No match

假定您已在字符串中平衡了雙引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM