I would like to find strings with regex pattern. For example:
# header ## smaller header
I would like to find two string set as follows.
# header
## smaller header
So I make the regex pattern as follows.
pattern = re.compile("(?:^#+|\s#+)\s")
With this pattern, I can find # header.
# header
But I can't find the string such as ## smaller header
How can I make the regex so that find two set of strings?
You can do the following
import re
p = re.compile(r"#+(\s\w+)+")
for m in p.finditer('# header ## smaller header'):
print(m.group())
which outputs
# header
## smaller header
import re
pattern = re.compile("#+[\s\w]+")
a = re.findall(pattern,'# header ## smaller header')
print (*a,sep="\n")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.