簡體   English   中英

不區分大小寫的用多個空格替換結尾 python

[英]Case insensitive replace end with multiple spaces python

我正在尋找從 sql 中刪除字符串“with Ur”。

stringval=" select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test ' with   Ur "
stringval=" select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test ' with   Ur "
stringval=" select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test ' with Ur "
stringval=" select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test ' with Ur "

預期結果 select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test '

在任何情況下刪除包含 ur 的末尾字符串以及它們之間的任何空格

insensitive_remove  = re.compile(re.escape('with ur$'), re.IGNORECASE
insensitive_remove.sub("",val.strip())

它有效但不對。 正則表達式是我缺少空格的東西

您不能簡單地要求最后一個 ' 字符然后拆分字符串嗎? 這應該工作

stringval=" select field1, field2 from table1 where field3=' WITH ur ' and field4 = ' test ' with   Ur "

str2 = "'";

lastIndexOfStr2 = stringval.rindex(str2)
print stringval[:lastIndexOfStr2 + 1]

你不需要re.escape ,事實上,它轉義太多了。 試試這個:

insensitive_remove = re.compile('\s*with\s+ur\s*$', re.IGNORECASE)

暫無
暫無

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

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