0

I'm trying to substitute the following string

word = 'hello contractor salary hour year' 

with the pattern

pattern = r'\b(contrator|per|salary|year|hour)'
word = re.sub(pattern, '', word, re.I)

but not all words are replaced I get the result 'hello hour year' Do you know how I can fix it?

Thanks for the answer from Kirill Bulygin

1 Answer 1

1

That's a common gotcha, use flags= (re.sub(pattern, '', word, flags=re.I)). Otherwise the artificial number designated by re.I is used as count.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it works, but would you mind explaining the reason of it?
yes, it's good, but if I add some other rules like (...| month(ly)?|...), it fails again, do you know why?
All I can tell from the information you gave is that it's unrelated to re.I or count. I would recommend to create a new question and include your code, the input, the actual output and the desired output.
thanks...I forget to pass the flags =... sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.