Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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_Split_Floating Point_Integer - Fatal编程技术网

Java 用于在字符串中拆分双数和整数的正则表达式

Java 用于在字符串中拆分双数和整数的正则表达式,java,regex,split,floating-point,integer,Java,Regex,Split,Floating Point,Integer,我需要一个正则表达式来分隔字符串的整数和双精度元素,如下例所示: String input = "We have the number 10 and 10.3, and i want to split both"; String[] splitted = input.split(/*REGULAR EXPRESSION*/); for(int i=0;i<splitted.length;i++) System.out.println("[" + i + "]" + " ->

我需要一个正则表达式来分隔字符串的整数和双精度元素,如下例所示:

String input = "We have the number 10 and 10.3, and i want to split both";
String[] splitted = input.split(/*REGULAR EXPRESSION*/);
for(int i=0;i<splitted.length;i++)
    System.out.println("[" + i + "]" + " -> \"" + splitted[i] + "\"");
String input=“我们有数字10和10.3,我想将两者分开”;
String[]splitted=input.split(/*正则表达式*/);
对于(int i=0;i“我们有数字”
  • [1] ->“10”
  • [2] ->“和”
  • [3] ->“10.3”
  • [4] ->“,我想将两者分开”
  • 有人能帮我吗?我将不胜感激。

    您需要将这些块与以下内容匹配:

    \D+|\d*\.?\d+
    

    详细信息

    • \D+
      -1个或多个字符(数字除外)
    • |
      -或
    • \d*\.?\d+
      -一个简单的整数或浮点数(可能会增强为
      [0-9]*[.]?[0-9]+(?:[eE][-+]?[0-9]+)?
      ,请参阅)
    A:

    String s=“我们有数字10和10.3,我想将两者分开”;
    Pattern=Pattern.compile(“\\D+\\D*\.?\\D+”);
    匹配器匹配器=模式匹配器;
    List res=new ArrayList();
    while(matcher.find()){
    res.add(matcher.group(0));
    } 
    系统输出打印项次(res);
    
    我不确定我是否理解这个问题。到底是什么问题?如果按数字拆分,您将丢失它们。正则表达式将是简单的
    \\d
    ,但输出没有数字。也许更复杂的正则表达式有助于更好地更改输出,以便其拆分更清晰correctly@XtremeBaumer字体好吧,这是。好吧,我想我想出了一个办法负浮点数/整数值的模式:。如果有人认为它有用,我将在答案中添加解释。
    String s = "We have the number 10 and 10.3, and i want to split both";
    Pattern pattern = Pattern.compile("\\D+|\\d*\\.?\\d+");
    Matcher matcher = pattern.matcher(s);
    List<String> res = new ArrayList<>();
    while (matcher.find()){
        res.add(matcher.group(0)); 
    } 
    System.out.println(res);