例如,基於Python中的邏輯關係做這個字符串patten替換的最快方法是什麼?
1. str1 = 'A>1 and A>=3 and B<2 and B<=3 and B<1 ...', should be substituted to:
str1 = 'A>=3 and B<1 ...';
2. str2=['A=1 and B<=2 ...', 'A=1 and B>2 ...'], should be substituted to:
str2=['A=1 ...'], where B is skipped
A,B可以是任何長度的法律蟒標識符。在str1和str2中都有未知數量的邏輯操作數。
通常的正則表達式搜索的方式是相當具有挑戰性的解決這個問題。任何黑客的想法?
編輯:
爲了使問題簡單化,我們考慮 '和' 只操作,所有操作數的排序爲一個字符串,即
'A<x and A<y and A<z' will always appear next to each other
我實在不明白你怎麼可以用正則表達式解決這個問題。我想說你需要解析你的字符串(如果你的格式被充分限制,這一步可以用正則表達式來完成),並對結果運行一個簡單的解釋器。 – fjarri