我想从HTML文件的一行中提取一个数字。我逐行阅读了文件,并希望使用正则表达式从有问题的行中获取数字。
我试过这样做:
def reportFileContents = new File("-path to file-").text
reportFileContents.eachLine{ line ->
def valueMatcher = /[n50 length] [*]([0-9]+)/
def matcher = (line =~ valueMatcher)
}但当我打印出我的'matcher‘时,我得到的结果如下:
java.util.regex.Matcher[pattern=[n50 length] [*]([0-9]+) region=0,22 lastmatch=]我在这里做错了什么?
发布于 2016-06-15 02:52:47
我猜你是说你正在尝试匹配一个像[n50 length] [*]123这样的字符串?如果是这样,请参见下面的示例。
def textWithMatches = '''blah blah blah [n50 length] [*]123 blah blah blah
|nothing here
|something else [n50 length] [*]321 something else'''.stripMargin()
def textWithNoMatches = 'no matches here'
assert (textWithMatches =~ /\[n50 length\] \[\*\](\d+)/).collect { it[1] } == [123, 321]
assert (textWithNoMatches =~ /\[n50 length\] \[\*\](\d+)/).collect { it[1] } == []https://stackoverflow.com/questions/37817154
复制相似问题