Python 使用正则表达式匹配5个数字,点,1个数字,点,5个数字
我正在尝试创建正则表达式以匹配以下模式:Python 使用正则表达式匹配5个数字,点,1个数字,点,5个数字,python,regex,python-2.7,Python,Regex,Python 2.7,我正在尝试创建正则表达式以匹配以下模式: 00000.1.17372 i、 e:5个数字点1个数字点5个数字 我尝试了以下重新匹配: find = re.match('d{5}.d{1}.d{5}', string) 在这方面: import re string = "{u'blabla': u'asdf', u'dd': u'a', u'cotry': u'jjK', u'l': u'/q/iii:00000.1.17372', u'stfe': u'', u'fdfhdiufhi':
00000.1.17372
i、 e:5个数字点1个数字点5个数字
我尝试了以下重新匹配:
find = re.match('d{5}.d{1}.d{5}', string)
在这方面:
import re
string = "{u'blabla': u'asdf', u'dd': u'a', u'cotry': u'jjK', u'l': u'/q/iii:00000.1.17372', u'stfe': u'', u'fdfhdiufhi': u'GB', u'y_name': u'Unitm', u'mw': u'00000.1.17372'}"
find = re.match('d{5}.d{1}.d{5}', string)
print find
但是,这似乎不起作用,因为输出是:
None
将以下内容与
re.findall
一起使用:
r'\b\d{5}\.\d\.\d{5}\b'
见
重点是:
- 要匹配数字,您需要使用
\d
- 必须转义一个点以匹配文字点
- 要匹配整个单词,您需要使用
单词边界,否则您将在\b
中找到类似字符串的5位数块匹配项2234567654
将返回所有非重叠匹配的列表(因为此模式中没有捕获组)re.findall
import re
regex = r"\b\d{5}\.\d\.\d{5}\b"
test_str = "{u'blabla': u'asdf', u'dd': u'a', u'cotry': u'jjK', u'l': u'/q/iii:00000.1.17372', u'stfe': u'', u'fdfhdiufhi': u'GB', u'y_name': u'Unitm', u'mw': u'00000.1.17372'}"
matches = re.findall(regex, test_str)
print(matches)
将以下内容与
re.findall
一起使用:
r'\b\d{5}\.\d\.\d{5}\b'
见
重点是:
- 要匹配数字,您需要使用
\d
- 必须转义一个点以匹配文字点
- 要匹配整个单词,您需要使用
单词边界,否则您将在\b
中找到类似字符串的5位数块匹配项2234567654
将返回所有非重叠匹配的列表(因为此模式中没有捕获组)re.findall
import re
regex = r"\b\d{5}\.\d\.\d{5}\b"
test_str = "{u'blabla': u'asdf', u'dd': u'a', u'cotry': u'jjK', u'l': u'/q/iii:00000.1.17372', u'stfe': u'', u'fdfhdiufhi': u'GB', u'y_name': u'Unitm', u'mw': u'00000.1.17372'}"
matches = re.findall(regex, test_str)
print(matches)
您想要的模式是:
\d{5}\.\d\.\d{5}
您需要对点进行转义,并对数字使用正确的标记,即
\d
您想要的模式是:
\d{5}\.\d\.\d{5}
您需要对点进行转义,并使用正确的数字标记,即
\d
re.match(r'^\d{5}\.\d{5}$',string)
使用搜索match
仅在字符串开头起作用。这与'ddddd#d#ddddd're.match(r'^\d{5}\.\d{5}$',string)匹配。
改用search
match
仅在字符串开头起作用。这与“ddddd#d#ddddd”匹配,当没有包含单词时,“匹配整个单词”是什么意思?:)抱歉误解。全字匹配表示在值之前或之后没有“单词”字符。“Word”字符包括字母、数字和下划线(~[a-zA-Z0-9\
-取决于re.UNICODE
标志)。当没有包含单词时,“匹配整个单词”是什么意思?:)抱歉误解。全字匹配表示在值之前或之后没有“单词”字符。“Word”字符包括字母、数字和下划线(~[a-zA-Z0-9\
-取决于re.UNICODE
标志)。