[英]How to make a Python regex pattern match a certain character at the start, or just be the start of the string?
This is my code so far I've been working on in Python 2.7 到目前为止,这是我在Python 2.7中进行的代码
import re
length_pattern = r"(?P<amount>[\d]{1,3}|(quarter)|(half)|(a[n]?))?[.]*(?P<type>(minute)|(hour)|(day)[s]?)?"
response='half hour '
length = re.search(length_pattern,response)
if length.group('amount')!=None or length.group('type')!=None:
print length.group('amount')
print length.group('type')
Ok, so I'm just needing this code to be able to take out the words "quarter", "half", "a", or just 1-3 digits. 好的,所以我只需要此代码即可取出单词“ quarter”,“ half”,“ a”或仅1-3个数字。 And store that in the amount named group.
并将其存储在名为group的数量中。
Then take out the word "minute", "hour", or "day" and store that in the type group. 然后取出单词“ minute”,“ hour”或“ day”并将其存储在类型组中。
import re
f = "(?P<amount>[\d]{1,3}|(quarter)|(half)|(a[n]?))?[.]*(?P<type>(minute)|(hour)|(day)[s]?)?"
find = re.search(r'(?P<amount>[\d]{1,3}|((.*))|', f)
do = find.group(1)
d = re.sub(do, '', f)
print "We taked out the word : %s" % do
and etc with other words 等用其他词
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.