Max Kim
Max Kim

Reputation: 1124

Regex matching string python

I wanted to match the following string:

strings = iamcool.iplay=ball?end

I want to remove items starting (including the ".") and up till "?", so I want to remove .iplay=ball, so I should have iamcool?end

This is the regex I have:

print re.sub(r'\.\.*?','', strings)

I am not sure how to stop at the "?"

Upvotes: 2

Views: 79

Answers (1)

ovgolovin
ovgolovin

Reputation: 13430

Use negated character class [^?] which matches anything except ?.

>>> re.sub(r'\.[^?]*', '', strings)
'strings = iamcool?end'

Upvotes: 2

Related Questions