开发者

Regexp : how to get every group of MatchData?

开发者 https://www.devze.com 2022-12-09 15:03 出处:网络
I have the followi开发者_StackOverflowng Regexp : regexp=/(\\w+) \\s* : \\s* (\\w+) \\s+ (.*) \\s* ;?/ix

I have the followi开发者_StackOverflowng Regexp :

regexp=/(\w+) \s* : \s* (\w+) \s+ (.*) \s* ;?/ix

And I am trying to get the captures:

names, direction, type = iotext.match(regexp).captures

This works fine for a single "x : in integer;" ,

but how could I also get all the groups of other match data in my file :

"x : in integer;
y : in logic;
z : in float;"


Your regex regexp is ok, it just only matches one occurance. If you want to match every occurance try

"x : in integer; y : in logic; z : in float;".scan(regexp)

which results in an array with 3 elements containing an array of each 3 matches, i.e.

 [ ["x", "in", "integer"], ["y", "in", "logic"], ["z", "in", "float"] ]
0

精彩评论

暂无评论...
验证码 换一张
取 消