Reputation: 9933
I am using regex to find time in a string:
re.match('.*(\d{2}-\d{2} \d{2}:\d{2})', "09-22 13:27")
anyway, this is ok, but if \n
exists, this return None
:
re.match('.*(\d{2}-\d{2} \d{2}:\d{2})', "\n09-22 13:27")
so why .*
can not match \n
? and how to deal with this?
Upvotes: 0
Views: 574
Reputation: 5668
You can exclude newline by using (?
st = "\n\n09-22 13:27\n"
import re
mo = re.findall(r'(?<!\S)\d{2}-\d{2}\D+\d{2}:\d{2}(?!\S)',st)
print(mo)
Results in:
['09-22 13:27']
Upvotes: 1
Reputation: 67988
print re.match('.*(\d{2}-\d{2} \d{2}:\d{2})', "\n09-22 13:27".strip())
^^^^^^^^
You can simply strip
the newline
.
Upvotes: 0
Reputation: 627468
why
.*
can not match\n
? and how to deal with this?
re
documentation says:
re.DOTALL
Make the.
special character match any character at all, including a newline; without this flag,.
will match anything except a newline.
You should note that .
matches any symbol but a newline. Use re.S
(re.DOTALL
) flag to match a newline with .
:
import re
obj = re.match('.*(\d{2}-\d{2} \d{2}:\d{2})', "\n09-22 13:27", re.S)
if obj:
print(obj.group(1))
See IDEONE demo
Upvotes: 4