如何动态获取正则表达式的分组?

yjsxy2005 2016-02-21 02:17:41
在一组不定长,但是有规律的字符串中,怎样动态获取正则表达式的各分组?

比如下面2个随便写的:
字符串1是"0521...3432........412...."
字符串1是"5.4..032.....333412.7482.........143.."

以上2个字符串的规律:一组(或个)数字后面跟一组(或个)小数点,不同之处是组数是不定的。
为匹配上面的规律,可以用一个正则表达式来匹配"^(\d+\.+)+$"

假设只需要提取出各组数据,我以为可以用正则表达式"^(?:(\d+)(?:\.+))+$"来匹配。

而实际确只能捕获到各字符串的最后一组,并没有像预想的那样工作。
字符串1只能捕获到412,字符串2只能捕获到143

如果将表达式写成定长的分组,如第1个"^(\d+)\.+(\d+)\.+(\d+)\.+$",肯定是可以分别获取到0521、3432、412这3组数据。
但是如果有上百个字符串,从1组到几十组的情况都有,就不能用定长的表达式来匹配了。

难道是正则表达式只会为实际存在的括号分组进行捕获,而不会用+*这些衍生出来的分组进行捕获?

望大神们指点,在线等。
...全文
486 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjsxy2005 2016-02-26
  • 打赏
  • 举报
回复
多谢指点
panghuhu250 2016-02-22
  • 打赏
  • 举报
回复
VBA的regex.Execute好像是会找到所有的匹配的. 看看这个:http://stackoverflow.com/questions/8146485/returning-a-regex-match-in-vba-excel Perl/ptyhon的正则再强, 不能在VBA下用也是白搭. 所以类似问题还是在VBA论坛问会更好.
yjsxy2005 2016-02-22
  • 打赏
  • 举报
回复
大概想到了个在vba环境的方法,应该可以,回头试试。 1、先用表达式1匹配整个字符串; 2、再用表达式2匹配各个数字,得到match集合。
yjsxy2005 2016-02-21
  • 打赏
  • 举报
回复
引用 1 楼 panghuhu250 的回复:
[quote=引用 楼主 yjsxy2005 的回复:] 难道是正则表达式只会为实际存在的括号分组进行捕获
对.
引用 楼主 yjsxy2005 的回复:
而不会用+*这些衍生出来的分组进行捕获?
((\d+)+)只有两个分组. 他不会先"扩展"成((\d+)(\d+)...)再匹配, 所以也没有"衍生出来"的分组. 你可以用re.findall.
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的话,还望指点,十分感谢。
panghuhu250 2016-02-21
  • 打赏
  • 举报
回复
引用 楼主 yjsxy2005 的回复:
难道是正则表达式只会为实际存在的括号分组进行捕获
对.
引用 楼主 yjsxy2005 的回复:
而不会用+*这些衍生出来的分组进行捕获?
((\d+)+)只有两个分组. 他不会先"扩展"成((\d+)(\d+)...)再匹配, 所以也没有"衍生出来"的分组. 你可以用re.findall.
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]: ['', '.', '..', '.....', '.', '.........', '..']

37,737

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧