Reputation: 5565
I am looking to match on a white space followed by anything except whitespace [i.e. letters, punctuation] at the start of a line in Python. For example:
` a` = True
` .` = True
` a` = False [double whitespace]
`ab` = False [no whitespace]
The rule re.match(" \w")
works except with punctuation - how do i include that?
Upvotes: 6
Views: 4830
Reputation: 341
import re
r = re.compile(r"(?<=^\s)[^\s]+.*")
print r.findall(" a")
print r.findall(" .")
print r.findall(" a")
print r.findall("ab")
output:
['a']
['.']
[]
[]
regex explanation:
Upvotes: 1
Reputation: 316
Remember the following:
\s\S
\s
is whitespace\S
is everything but whitespaceUpvotes: 13