[英]java String.split(regex) design
I'm importing a file with umpteen lines of "##,##"
. 我正在导入带有多行"##,##"
。 Each number can be one or two digits. 每个数字可以是一个或两个数字。
I'd like to use String.split(regex)
to get the two numbers without the adjacent quote marks. 我想使用String.split(regex)
来获得两个数字而没有相邻的引号。
Understanding that I could nibble off the first and last character and use a non-regex split, I'm hoping that there is a regular expression that will make this more graceful. 了解到我可以蚕食第一个和最后一个字符并使用非正则表达式拆分后,我希望有一个正则表达式可以使此样式更加优美。
Suggestions? 有什么建议吗?
EDIT: 编辑:
In: "12,3"
Out: 12
3
How about using a regexp \\"(d+),(d+)\\"
. 如何使用正则表达式\\"(d+),(d+)\\"
。 Then using Pattern.matcher(input)
instead of String.split
, and obtaining your digits by Matcher.group(int)
. 然后使用Pattern.matcher(input)
代替String.split
,并通过Matcher.group(int)
获得您的数字。
Please consider following snippet: 请考虑以下代码段:
String line = "\"1,31\"";
Pattern pattern = Pattern.compile("\"(\\d+),(\\d+)\"");
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
int firstNumber = Integer.parseInt(matcher.group(1));
int secondNumber = Integer.parseInt(matcher.group(2));
// do whatever with the numbers
}
You can remove all double-quotes characters in each line then split the string by , 您可以删除每行中的所有双引号字符,然后将字符串除以,
String toSplit = "\"##,##\"";
String[] splitted = toSplit.replaceAll("\"", "").split(",");
Using \\"
in the toSplit
string to simulate the "##,##"
string. 在toSplit
字符串中使用\\"
模拟"##,##"
字符串。
You could split at the quotes as well but that would result in an array of length 4. Unfortunately, there's no way of splitting a string and removing others characters from the same string in one call using String#split
. 您也可以在引号之间进行拆分,但这将导致长度为4的数组。不幸的是,在使用String#split
一次调用中,无法拆分字符串并从同一字符串中删除其他字符。
As an alternative, you could use Apache's StringUtils
: 或者,您可以使用Apache的StringUtils
:
String[] n = StringUtils.removeStart( StringUtils.removeEnd( "##,##", "\""), "\"").split(",");
Edit: as a side note, using StringUtils
would allow for missing quotes at the start or end of the input string. 编辑:作为附带说明,使用StringUtils
将允许在输入字符串的开头或结尾缺少引号。 If you're sure they're always present, a simple substring(...)
might be sufficient. 如果您确定它们始终存在,那么简单的substring(...)
就足够了。 (credits go to @Ingo) (学分去@Ingo)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.