Case insensitive replace end with multiple spaces python

Question:

I am looking to remove the string "with Ur" from the sql.

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 "

expected result
select field1, field2 from table1 where field3=’ WITH ur ‘ and field4 = ‘ test ‘

remove the string at end containing ‘with ur’ in any case and any white space between them

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

it works but not right. Regex is something i am missing for whitespaces

Asked By: Rafa

||

Answers:

Couldn’t you simply ask for the last ‘ character and then split the String?
This should work

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

str2 = "'";

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

You don’t need the re.escape, in fact, it escapes too much.
Try this instead:

insensitive_remove = re.compile('s*withs+urs*$', re.IGNORECASE)
Answered By: Wups
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.