37,737
社区成员
发帖
与我相关
我的任务
分享
In [62]: s = "5.4..032.....333412.7482.........143.."
In [63]: import re
In [64]: re.findall("\d+", s)
Out[64]: ['5', '4', '032', '333412', '7482', '143']
In [65]: re.findall("\d+", "1..2")
Out[65]: ['1', '2']
In [66]: re.findall("(\d+)", s) # same as "\d+"
Out[66]: ['5', '4', '032', '333412', '7482', '143']
In [67]: re.findall("(\d+)\.\.", s) # all numbers that has two dots after it
Out[67]: ['4', '032', '7482', '143']
In [68]: re.findall("(\d+)(\.\.)", s) # capture both the numbers and the dots
Out[68]: [('4', '..'), ('032', '..'), ('7482', '..'), ('143', '..')]
In [72]: re.findall?
Signature: re.findall(pattern, string, flags=0)
Docstring:
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
# 顺便提一下re.split
In [73]: re.split("(\d+)", s) # split and capture the separators
Out[73]:
['',
'5',
'.',
'4',
'..',
'032',
'.....',
'333412',
'.',
'7482',
'.........',
'143',
'..']
In [74]: re.split("\d+", s) # split without separators
Out[74]: ['', '.', '..', '.....', '.', '.........', '..']
[/quote]
多谢楼上大神指点,没有接触过python,但是看着好像re.findall很好用。
对于实际要用的字符串,中文数字英文标点及多个空格分隔组成的,就应该可以用re.findall("\S+", s)来获取。
本意是想在excel的vba环境写的,看了正则表达式的资料在用perl做示例比较多,所以到包含的perl的版块发帖。
vba环境下好像没有类似的方法可以操作,这个问题困扰了很久,一直没搞定。
如果碰巧大神也熟悉vba的话,还望指点,十分感谢。
In [62]: s = "5.4..032.....333412.7482.........143.."
In [63]: import re
In [64]: re.findall("\d+", s)
Out[64]: ['5', '4', '032', '333412', '7482', '143']
In [65]: re.findall("\d+", "1..2")
Out[65]: ['1', '2']
In [66]: re.findall("(\d+)", s) # same as "\d+"
Out[66]: ['5', '4', '032', '333412', '7482', '143']
In [67]: re.findall("(\d+)\.\.", s) # all numbers that has two dots after it
Out[67]: ['4', '032', '7482', '143']
In [68]: re.findall("(\d+)(\.\.)", s) # capture both the numbers and the dots
Out[68]: [('4', '..'), ('032', '..'), ('7482', '..'), ('143', '..')]
In [72]: re.findall?
Signature: re.findall(pattern, string, flags=0)
Docstring:
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
# 顺便提一下re.split
In [73]: re.split("(\d+)", s) # split and capture the separators
Out[73]:
['',
'5',
'.',
'4',
'..',
'032',
'.....',
'333412',
'.',
'7482',
'.........',
'143',
'..']
In [74]: re.split("\d+", s) # split without separators
Out[74]: ['', '.', '..', '.....', '.', '.........', '..']