【发布时间】:2016-12-12 08:44:09
【问题描述】:
我有一个包含某些类名 + 方法调用的字符串。我想提取这些,但我为此编写的正则表达式返回的匹配项比预期的要多。
#!/usr/bin/perl
$someInput = "Lorem ipsum dolor sit CLASS3.aMethod.anotherMethod amet, consetetur sadipscing CLASS1.bMethod elitr, sed diam nonumy eirmod";
@matches = ($someInput =~ /((CLASS1|CLASS2|CLASS3)(\.[A-Za-z0-9_]+){1,})/g);
foreach my $match (@matches) {
print $match . "\n";
}
除了“CLASS.methodOne.methodTwo...”形式的一个匹配之外,它还分别匹配类名和方法。请看示例结果:
CLASS3.aMethod.anotherMethod
CLASS3
.anotherMethod
CLASS1.bMethod
CLASS1
.bMethod
我真正想要的:
CLASS3.aMethod.anotherMethod
CLASS1.bMethod
如果有人可以帮助我,我将不胜感激。简短的解释也很好:) 谢谢!
【问题讨论】:
-
删除所有无用的捕获组,并在需要时使用非捕获组。仅显示您想要的组。
标签: regex perl pattern-matching