1

I current have a regular expression defined as follows:

>>> import re
>>> regex = re.compile("(\d+:)+(\d+)")
>>> search_results = regex.search("52345:54325432:555:443:3:33")
>>> search_results.groups()
('3:', '33')

I know I could do

>>> "52345:54325432:555:443:3:33".split(":")

for splitting each item into tokens, but I want to know how I could achieve this using a regex.

4

4 回答 4

1

re.findall如果您想要所有匹配项,请使用,re.search在第一个匹配项处停止。:

>>> strs = "52345:54325432:555:443:3:33"
>>> re.findall(r"(\d+):(\d+)",strs)
[('52345', '54325432'), ('555', '443'), ('3', '33')]

如果您想要与 then 完全相同的结果,str.split您可以这样做:

>>> re.split(r":",strs)
['52345', '54325432', '555', '443', '3', '33']
>>> re.findall(r"[^:]+",strs)
['52345', '54325432', '555', '443', '3', '33']
于 2013-07-09T16:18:08.170 回答
0

你应该用它split来解决这个问题。

findall将适用于任何有效的字符串。不幸的是,它也适用于任何无效的字符串。如果那是您想要的,那很好;但可能你想知道是否有错误。

例子:

>>> import re
>>> digits = re.compile("\d+")
>>> digits.findall("52345:54325432:555:443:3:33")
['52345', '54325432', '555', '443', '3', '33']
>>> digits.findall("52345:54325.432:555:443:3:33")
['52345', '54325', '432', '555', '443', '3', '33']
>>> digits.findall(""There are 2 numbers and 53 characters in this string."")
['2', '53']

当然,如果您确定只使用该re 模块,您可以先匹配然后拆分:

>>> valid = re.compile("(?:\d+:)*\d+$")
>>> digits = re.compile("\d+")
>>> s = "52345:54325432:555:443:3:33"
>>> digits.findall(s) if valid.match(s) else []

相比之下:

>>> [int(n) for n in "52345:54325432:555:443:3:33".split(":")]
[52345, 54325432, 555, 443, 3, 33]
>>> [int(n) for n in "52345:54325.432:555:443:3:33".split(":")]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '54325.432'

>>> [int(n)
...  for n in "There are 2 numbers and 53 characters in this string.".split(":")]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10:
  'There are 2 numbers and 53 characters in this string.'
于 2013-07-09T17:04:45.547 回答
0
(?<test>[0-9]+):

这是正则表达式。您将要做的是:例如,您的字符串在 str 中:

var str = "52345:54325432:555:443:3:33";

那么你将不得不在while循环中将此字符串与正则表达式匹配

while(RegexMatch.Success){

// 这里的操作 }

第一个值:即:52345 将在:

var first = RegexMatch.Groups["test"].Value;

进行第一次更改。

注意:这不是匹配正则表达式等的确切代码,而是一个伪代码。希望你能理解。我附上图片以显示正则表达式中的组。 在此处输入图像描述

于 2013-07-09T18:52:30.343 回答
0

看看这是否有帮助...

>>> pat = r'(\d+(?=\:)|\d+$)'
>>> regexp = re.compile(pat)
>>> m = regexp.findall("52345:54325432:555:443:3:33")
>>> m
['52345', '54325432', '555', '443', '3', '33']
>>>
于 2013-07-09T16:26:01.393 回答