我试图创建一个匹配器来匹配一个正则表达式并返回一个特定的索引,但是尽管尝试了代码的许多变体,它要么抛出一个异常,要么只打印单个字符而不是整个单词。我找到的所有例子都与我正在做的相似,但我的结果看起来不像例子。代码如下:
def RAW = """
policer-profile "GD-1"
bandwidth cir 4992 cbs 32767 eir 4992 ebs 32767
traffic-type all
compensation 0
exit
policer-profile "EIR-1"
bandwidth cir 0 cbs 0 eir 9984 ebs 32767
traffic-type all
compensation 0
exit
shaper-profile "Shaper1"
bandwidth cir 999936 cbs 65535
compensation 0
exit
"""
RAW.split("\n").each() { line ->
def matcher = line =~ /bandwidth cir \d+ cbs \d+/
if (matcher) {
println line[0][2]
}
}
我一直得到“index out of range”或者它只是为每行打印单词“bandwidth”中的“n”(第三个字符),而不是“cir”(第三个单词)之后的数值。任何帮助将不胜感激。先谢了。
1条答案
按热度按时间x8goxv8g1#
我稍微修改了一下脚本:
你有一个错误的正则表达式(空格在开头,也不匹配行尾),并记住输出从
matcher
而不是从line
获取的组。现在它应该工作。