Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.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/16.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 - Fatal编程技术网

Java 使用哪个正则表达式?

Java 使用哪个正则表达式?,java,regex,Java,Regex,我有这样的表达: -3-5 or -3--5 or 3-5 or 3-+5 or -3-+5 我需要提取数字,在它们之间用“-”号分开,即在上述情况下,我需要, -3和5,-3和-5,3和5,3和+5,-3和+5。 我试过使用这个: String s[] = str.split("[+-]?\\d+\\-[+-]?\\d+"); int len = s.length; for(int i=0;i<len;i++)System.out.println(s[i]);

我有这样的表达:

-3-5
or -3--5
or 3-5
 or  3-+5
or -3-+5
我需要提取数字,在它们之间用“-”号分开,即在上述情况下,我需要, -3和5,-3和-5,3和5,3和+5,-3和+5。 我试过使用这个:

String s[] = str.split("[+-]?\\d+\\-[+-]?\\d+");
    int len = s.length;
       for(int i=0;i<len;i++)System.out.println(s[i]);
String s[]=str.split(“[+-]?\\d+\-[+-]?\\d+”);
int len=s.长度;

对于(int i=0;i尝试使用此正则表达式拆分:

str.split("\\b-")
单词边界
\b
应仅在数字之前或之后匹配,以便与
-
组合使用时,仅匹配以下
-
作为范围指示器:

-3-5, -3--5 , 3-5,3-+5,-3-+5
  ^     ^      ^   ^     ^

尝试使用此正则表达式拆分:

str.split("\\b-")
单词边界
\b
应仅在数字之前或之后匹配,以便与
-
组合使用时,仅匹配以下
-
作为范围指示器:

-3-5, -3--5 , 3-5,3-+5,-3-+5
  ^     ^      ^   ^     ^

交叉发布到forums.sun.com


这不是RES本身的一个工作。需要一个扫描仪来返回运算符和数字,以及表达式解析器。考虑-3–-5。< /P> < P>交叉点到论坛。Sun.com。< /P>


这不是Res本身的一个工作。你需要一个扫描仪来返回运算符和数字,以及表达式解析器。考虑-3 ------- 5。< /P> < p>你的表达式很好。尽管你试图将表达式匹配到你的字符串中,但不是拆分。 下面是一些可以利用表达式获得所需内容的代码:

String a = "-90--80";
Pattern x = Pattern.compile("([+-]?\\d+)\\-([+-]?\\d+)");
Matcher m = x.matcher(a);
if(m.find()){
 System.out.println(m.group(1));
 System.out.println(m.group(2));
}

您的表达式还可以。但拆分不是选择,因为您正在尝试将表达式与字符串匹配-而不是使用它拆分字符串:

下面是一些可以利用表达式获得所需内容的代码:

String a = "-90--80";
Pattern x = Pattern.compile("([+-]?\\d+)\\-([+-]?\\d+)");
Matcher m = x.matcher(a);
if(m.find()){
 System.out.println(m.group(1));
 System.out.println(m.group(2));
}

真的很好!我本可以放弃split,最终找到一个长的正则表达式匹配器。你的更好。有人能解释一下我试过的正则表达式有什么问题吗?@pranay-你的正则表达式很好,但在这种情况下,夹板是错误的。你可能想
匹配
,并为这两个数字添加捕获组。@Kobi:你能给我一个吗一个例子please@pranay-嗯,现在不是Java,但这应该更清楚:。请注意,您的正则表达式是正确的,我只是添加了组以便捕获数字。真的很好!我会放弃split并最终使用一些长正则表达式匹配器。您的更好。有人能解释一下我使用的正则表达式有什么问题吗里德?@pranay-你的正则表达式很好,但在这种情况下夹板是错误的。你可能想
匹配
,并为这两个数字添加捕获组。@Kobi:你能举个例子吗please@pranay-嗯,目前不是用Java,但这应该让它更清楚:。请注意,您的正则表达式是正确的,我只是添加了组,以便能够捕获数字。