Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用正则表达式并使用正则表达式拆分字符串_Java_Regex_String_Split - Fatal编程技术网

Java使用正则表达式并使用正则表达式拆分字符串

Java使用正则表达式并使用正则表达式拆分字符串,java,regex,string,split,Java,Regex,String,Split,这似乎是一个基本的东西,但我似乎不能让我的头周围regex的我从来没有真正使用过他们之前,现在我已经遇到了一个时间,他们将是有用的 在过去的一个小时里,我看了一些例子和过去的问题,但还是不明白。我的问题是我有一个字符串 "(2 h 9 min from now) | +18.7 feet" 我想把它分成两条线 String a = "2 h 9 min from now"; 及 如何使用正则表达式拆分字符串并在其他字符串中使用正则表达式 到目前为止,我提出了: stringx.split("

这似乎是一个基本的东西,但我似乎不能让我的头周围regex的我从来没有真正使用过他们之前,现在我已经遇到了一个时间,他们将是有用的

在过去的一个小时里,我看了一些例子和过去的问题,但还是不明白。我的问题是我有一个字符串

"(2 h 9 min from now) | +18.7 feet"
我想把它分成两条线

String a = "2 h 9 min from now";

如何使用正则表达式拆分字符串并在其他字符串中使用正则表达式

到目前为止,我提出了:

stringx.split("(%s) | +%s \n");

但是我不知道如何将%s(如果正确的话)放入regex之外的字符串中,您可以使用:

String s = "(2 h 9 min from now) | +18.7 feet";
Pattern p = Pattern.compile("^\\(([^)]+)\\)\\s*\\|\\s*\\+(.*)$");
Matcher m = p.matcher(s);
if (m.find())               
    System.out.println(m.group(1) + " :: " + m.group(2)); 

 // 2 h 9 min from now :: 18.7 feet

当您想要删除一些字符(
()
+
)时,最安全的方法是与和类匹配的标准正则表达式:

输出:

输入:(2小时9分钟后)|+18.7英尺
a:2小时9分钟后
b:18.7英尺

我将分两步完成

  • 首先,你要分开
  • 然后,你消毒
例如:

// the original text
String text = "(2 h 9 min from now) | +18.7 feet";
// splitting on the "|" separator
String[] splitted = text.split("\\|");
// printing the raw "split" array
System.out.println("Raw: " + Arrays.toString(splitted));
// iterating over the raw elements of the array
for (String split: splitted) {
    // replacing all "raw" strings with the group composed of 
    // word characters in between non word characters (if any)
    System.out.println(split.replaceAll("^\\W*(.+?)\\W*$", "$1"));
}
输出:

Raw: [(2 h 9 min from now) ,  +18.7 feet]
2 h 9 min from now
18.7 feet

不是最干净的解决方案,但它会给你一个开始

您似乎将
split
与正则表达式模式匹配混淆。OP似乎也希望去掉括号和加号。注意:如果您确实想使用
String\split()
,可以使用。虽然我不建议这样做,因为输入中的细微更改可能会导致意外的输出,而且代码中也不清楚您的意图(是什么让维护它的人的工作——可能是您自己——变得更加困难)。使用正则表达式仍然看起来很混乱。它是从url api的bufferedReader获取的一段信息。expect from string.replace the string.split您还建议我使用什么?编辑-是的,我知道严格地说,我应该使用xml解析器,但是我无法让它正常工作,并且看到我可以在20行(比解析器少60行)中完成同样的工作,我想探索其他选项,使它更有效并进一步压缩它。我仍然建议使用正则表达式(以及模式/匹配器类)。我知道这看起来有点混乱,但您的场景实际上是正则表达式的一个用例。其他方法,如
split()
replace()
,可能会发送错误的消息(它们会使代码更难阅读)。然而,使用正则表达式,您可以使用方法使代码不那么混乱。例如在我看来,这是非常干净的。噢,如果你的目标是减少代码行数,那么
split()
example()是无与伦比的,尽管每个人都很接近。是的,我只是想探索其他选项,只是出于好奇,在速度上是否存在一些差异,或者仅仅是毫秒的问题?
StringTokenizer stringtokenizer = new StringTokenizer("Your string", "|");
while (stringtokenizer.hasMoreElements()) {
System.out.println(stringtokenizer.nextToken());
}
public static void main (String[] args) {
    String input= "(2 h 9 min from now) | +18.7 feet";
    System.out.println("Input: "+ input);
    Pattern p = Pattern.compile("\\(([^)]+)\\) \\| \\+(\\d+\\.\\d feet)");
    Matcher m = p.matcher(input);
    String a = null, b = null;
    if (m.find()) {
        a = m.group(1);
        b = m.group(2);
    }
    System.out.println("a: "+ a);
    System.out.println("b: "+ b);
}
// the original text
String text = "(2 h 9 min from now) | +18.7 feet";
// splitting on the "|" separator
String[] splitted = text.split("\\|");
// printing the raw "split" array
System.out.println("Raw: " + Arrays.toString(splitted));
// iterating over the raw elements of the array
for (String split: splitted) {
    // replacing all "raw" strings with the group composed of 
    // word characters in between non word characters (if any)
    System.out.println(split.replaceAll("^\\W*(.+?)\\W*$", "$1"));
}
Raw: [(2 h 9 min from now) ,  +18.7 feet]
2 h 9 min from now
18.7 feet