为什么 split 不能在这里工作?
Why isn't split working here?
我正在尝试获取从 tsv 传递的文件名并将其拆分为一个数组,如下所示:
new File("filenames.tsv").eachLine( { String file_iter ->
println file_iter
def details = file_iter.split(".")
println details
})
printlns 的输出:
stad.all.16jan15.TP.pwpv
[]
为什么数组是空的?我确定我遗漏了一些明显的东西。
String#split 将正则表达式作为参数。
.
是正则表达式特殊字符,见regex tutorial:
Because we want to do more than simply search for literal pieces of text, we need to reserve certain characters for special use. In the regex flavors discussed in this tutorial, there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1+1=2. Otherwise, the plus sign has a special meaning.
在 Java 字符串文字语法中,反斜杠是转义字符,因此在 Java 中,您必须对反斜杠进行两次转义才能使其正确显示。 Groovy adds options for specifying string literals 这样您就可以使用斜线字符串语法避免双重转义。
以 groovysh 为例:
groovy:000> s = 'asdf.zxcv.qwerty'
===> asdf.zxcv.qwerty
如果没有转义,句点意味着所有内容都是分隔符,因此结果为空
groovy:000> s.split('.')
===> []
使用 Java 双转义语法
groovy:000> s.split('\.')
===> [asdf, zxcv, qwerty]
使用斜线字符串文字语法
groovy:000> s.split(/\./)
===> [asdf, zxcv, qwerty]
请注意,即使闭包是 eachLine 的参数,您也不需要将闭包括在括号中,您可以将其写为:
new File('filenames.tsv').eachLine { String file_iter ->
println file_iter
def details = file_iter.split(/\./)
println details
}
我正在尝试获取从 tsv 传递的文件名并将其拆分为一个数组,如下所示:
new File("filenames.tsv").eachLine( { String file_iter ->
println file_iter
def details = file_iter.split(".")
println details
})
printlns 的输出:
stad.all.16jan15.TP.pwpv
[]
为什么数组是空的?我确定我遗漏了一些明显的东西。
String#split 将正则表达式作为参数。
.
是正则表达式特殊字符,见regex tutorial:
Because we want to do more than simply search for literal pieces of text, we need to reserve certain characters for special use. In the regex flavors discussed in this tutorial, there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash. If you want to match 1+1=2, the correct regex is 1+1=2. Otherwise, the plus sign has a special meaning.
在 Java 字符串文字语法中,反斜杠是转义字符,因此在 Java 中,您必须对反斜杠进行两次转义才能使其正确显示。 Groovy adds options for specifying string literals 这样您就可以使用斜线字符串语法避免双重转义。
以 groovysh 为例:
groovy:000> s = 'asdf.zxcv.qwerty'
===> asdf.zxcv.qwerty
如果没有转义,句点意味着所有内容都是分隔符,因此结果为空
groovy:000> s.split('.')
===> []
使用 Java 双转义语法
groovy:000> s.split('\.')
===> [asdf, zxcv, qwerty]
使用斜线字符串文字语法
groovy:000> s.split(/\./)
===> [asdf, zxcv, qwerty]
请注意,即使闭包是 eachLine 的参数,您也不需要将闭包括在括号中,您可以将其写为:
new File('filenames.tsv').eachLine { String file_iter ->
println file_iter
def details = file_iter.split(/\./)
println details
}