The result list contains single spaces when splitting a string with re.split(“( )+”) – is there a better way?

By using (,), you are capturing the group, if you simply remove them you will not have this problem. >>> str1 = “a b c d” >>> re.split(” +”, str1) [‘a’, ‘b’, ‘c’, ‘d’] However there is no need for regex, str.split without any delimiter specified will split this by whitespace for you. This would … Read more

How to match a newline character in a raw string?

In a regular expression, you need to specify that you’re in multiline mode: >>> import re >>> s = “””cat … dog””” >>> >>> re.match(r’cat\ndog’,s,re.M) <_sre.SRE_Match object at 0xcb7c8> Notice that re translates the \n (raw string) into newline. As you indicated in your comments, you don’t actually need re.M for it to match, but … Read more

re.sub replace with matched content

Simply use \1 instead of $1: In [1]: import re In [2]: method = ‘images/:id/huge’ In [3]: re.sub(r'(:[a-z]+)’, r'<span>\1</span>’, method) Out[3]: ‘images/<span>:id</span>/huge’ Also note the use of raw strings (r’…’) for regular expressions. It is not mandatory but removes the need to escape backslashes, arguably making the code slightly more readable.