Java String.split() 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9856916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:22:59  来源:igfitidea点击:

Java String.split() Regex

javaregexstringsplit

提问by user677786

I have a string:

我有一个字符串:

String str = "a + b - c * d / e < f > g >= h <= i == j";

I want to split the string on all of the operators, but include the operators in the array, so the resulting array looks like:

我想在所有运算符上拆分字符串,但将运算符包含在数组中,因此结果数组如下所示:

[a , +,  b , -,  c , *,  d , /,  e , <,  f , >,  g , >=,  h , <=,  i , ==,  j]

I've got this currently:

我目前有这个:

public static void main(String[] args) {
    String str = "a + b - c * d / e < f > g >= h <= i == j";
    String reg = "((?<=[<=|>=|==|\+|\*|\-|<|>|/|=])|(?=[<=|>=|==|\+|\*|\-|<|>|/|=]))";

    String[] res = str.split(reg);
    System.out.println(Arrays.toString(res));
}

This is pretty close, it gives:

这非常接近,它给出:

[a , +,  b , -,  c , *,  d , /,  e , <,  f , >,  g , >, =,  h , <, =,  i , =, =,  j]

Is there something I can do to this to make the multiple character operators appear in the array like I want them to?

我可以做些什么来使多个字符运算符像我希望的那样出现在数组中?

And as a secondary question that isn't nearly as important, is there a way in the regex to trim the whitespace off from around the letters?

作为一个不太重要的次要问题,正则表达式中有没有办法从字母周围修剪空白?

采纳答案by IchBinKeinBaum

String[] ops = str.split("\s*[a-zA-Z]+\s*");
String[] notops = str.split("\s*[^a-zA-Z]+\s*");
String[] res = new String[ops.length+notops.length-1];
for(int i=0; i<res.length; i++) res[i] = i%2==0 ? notops[i/2] : ops[i/2+1];

This should do it. Everything nicely stored in res.

这应该这样做。一切都很好地存储在res.

回答by Chris White

Can you invert your regex so split by the non operation characters?

你能反转你的正则表达式,这样被非操作字符分割吗?

String ops[] = string.split("[a-z]")
// ops == [+, -, *, /, <, >, >=, <=, == ]   

This obviously doesn't return the variables in the array. Maybe you can interleave two splits (one by the operators, one by the variables)

这显然不会返回数组中的变量。也许你可以交错两个分割(一个由运算符,一个由变量)

回答by user unknown

str.split (" ") 
res27: Array[java.lang.String] = Array(a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j)

回答by Andrew Morton

You could split on a word boundary with \b

您可以使用 \b 在单词边界上拆分

回答by dat_clover

    String str = "a + b - c * d / e < f > g >= h <= i == j";
    String reg = "\s*[a-zA-Z]+";

    String[] res = str.split(reg);
    for (String out : res) {
        if (!"".equals(out)) {
            System.out.print(out);
        }
    }

Output : + - * / < > >= <= ==

输出:+ - * / < > >= <= ==

回答by sQuirel

You could also do something like:

您还可以执行以下操作:

String str = "a + b - c * d / e < f > g >= h <= i == j";
String[] arr = str.split("(?<=\G(\w+(?!\w+)|==|<=|>=|\+|/|\*|-|(<|>)(?!=)))\s*");

It handles white spaces and words of variable length and produces the array:

它处理空格和可变长度的单词并生成数组:

[a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j]