Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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/19.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 String.split()正则表达式_Java_Regex_String_Split - Fatal编程技术网

Java String.split()正则表达式

Java String.split()正则表达式,java,regex,string,split,Java,Regex,String,Split,我有一个字符串: String str = "a + b - c * d / e < f > g >= h <= i == j"; String str=“a+b-c*d/eg>=hg>=h你能把你的正则表达式倒过来按非操作字符分割吗 String ops[] = string.split("[a-z]") // ops == [+, -, *, /, <, >, >=, <=, == ] String ops[]=String.spli

我有一个字符串:

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

String str=“a+b-c*d/eg>=hg>=h你能把你的正则表达式倒过来按非操作字符分割吗

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

res27:Array[java.lang.String]=Array(a,+,b,,,c,*,d,/,e,g,>=,h,您可以使用\b

String[]ops=str.split(\\s*[a-zA-Z]+\\s*)在单词边界上拆分;
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];
字符串[]notops=str.split(“\\s*[^a-zA-Z]+\\s*”; String[]res=新字符串[ops.length+notops.length-1];
对于(inti=0;i
String str=“a+b-c*d/eg>=h您还可以执行以下操作:

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

String str="a+b-c*d/eg>=h您可以在示例表达式中用空格分割,以得到您想要的结果。对于您的第二个问题:String有一个修剪函数:@Jeffrey:空格不一定在那里。为了便于阅读,我在那里有空格,但它可以是空格的任意组合,也可以没有。谢谢您的想法ough!@user306848:是的,我知道trim,我只是好奇它在正则表达式中是否可行。不过谢谢你的提示!你尝试过了吗?你会有问题的。好的,我承认,我在.NET中测试过它,它很有效。删除空条目应该很简单,删除字符串中的空格肯定很容易用.repa完成ceAll在应用正则表达式之前。虽然不是精确的解决方案,但它确实给了我有效的想法!谢谢!我将编辑解决方案的主要帖子!是的,这很有效,只需从数组中去掉前导元素(它是空的)回来后,这似乎是最好的方法。我想在正则表达式中这样做,但这将非常有效。谢谢!有什么解释吗?
String ops[] = string.split("[a-z]")
// ops == [+, -, *, /, <, >, >=, <=, == ]   
str.split (" ") 
res27: Array[java.lang.String] = Array(a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j)
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];
    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);
        }
    }
String str = "a + b - c * d / e < f > g >= h <= i == j";
String[] arr = str.split("(?<=\\G(\\w+(?!\\w+)|==|<=|>=|\\+|/|\\*|-|(<|>)(?!=)))\\s*");
[a, +, b, -, c, *, d, /, e, <, f, >, g, >=, h, <=, i, ==, j]