Regexp:如何获得每一组MatchData?
我有以下Regexp:
regexp=/(\w+) \s* : \s* (\w+) \s+ (.*) \s* ;?/ix
而我正试图获取捕获:
names, direction, type = iotext.match(regexp).captures
这适用于单个“x:in integer;” ,
但我怎么能在我的文件中获取所有其他匹配数据组:
"x : in integer; y : in logic; z : in float;"
你的正则regexp
正则regexp
是好的,它只匹配一个出现。 如果你想匹配每一次出现尝试
"x : in integer; y : in logic; z : in float;".scan(regexp)
这导致一个包含3个元素的数组,其中包含每3个匹配的数组,即
[ ["x", "in", "integer"], ["y", "in", "logic"], ["z", "in", "float"] ]