开发者

Regexp : how to get every group of MatchData?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜