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

Java:根据前面出现的字符打印特定的子字符串,如果在后面遇到字母,则结束?

Java:根据前面出现的字符打印特定的子字符串,如果在后面遇到字母,则结束?,java,string,substring,Java,String,Substring,我有一个输入字符串,比如说它是111.112.111.0.0>111.33.44.55.11 pxs ikf len 3348,它有时也可能通过流输入为111.112.111.0.0>111.33.44.55.11 pxs ikf len 3348 bbbtt 如何捕获via子字符串中“len”之后出现的数字?使用system.out.println(stringStr.substring(stringStr.lastIndexOf(“”+1)只会打印3348,如果它是上面字符串的最后一部分,但

我有一个输入字符串,比如说它是
111.112.111.0.0>111.33.44.55.11 pxs ikf len 3348
,它有时也可能通过流输入为
111.112.111.0.0>111.33.44.55.11 pxs ikf len 3348 bbbtt

如何捕获via子字符串中“len”之后出现的数字?使用
system.out.println(stringStr.substring(stringStr.lastIndexOf(“”+1)
只会打印
3348
,如果它是上面字符串的最后一部分,但如果BBBTTT出现,它将不起作用


如何打印子字符串的数字,特别是在“len”之后,但不包括后面的数字(可能是0-9999,而不是更多),因此后面没有任何字符?

您可以使用
拆分方法进行此操作

        String s = "111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348 BBBTTT";
    s = s.split("len")[1];
    s = s.replaceAll("\\D+", "");
    System.out.println(s);
或者只是

s = s.split("len")[1].replaceAll("\\D+", "");

您可以在此处使用正则表达式,但还有其他方法:

  • 确定字符串中“len”的索引
  • 再加上4,这就是你的数字开始的地方
  • 从那里做一个子串
  • 该字符串要么包含“just digits”,要么后面跟一个空格的数字。因此,您可以执行另一个indexOf()调用来计算是否有空格;如果有,也可以丢弃该空格后面的所有内容;例如:

    String numberWithChars = "3348 BBBTTT";
    int indexOfSpace = numberWithChars.indexOf(" ");
    if  (indexOfSpace > 0) {
       String pureNumber = numberWithChars.substring(0, indexOfSpace);
    } else {
       String pureNumber = numberWithChars;
    }
    

    希望有帮助。

    使用正则表达式。模式类似于“len([\d]+)”


    使用正则表达式的两个字符串的示例:

            String s1 = "111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348";
            String s2 = "111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3349 BBBTTT";
    
            Pattern pattern = Pattern.compile(".*len (\\d*).*");
            Matcher m1 = pattern.matcher(s1);
            Matcher m2 = pattern.matcher(s2);
            if (m1.matches()) {
                System.out.println(m1.group(1));
            }
            if (m2.matches()) {
                System.out.println(m2.group(1));
            }
    
    您可以将其放在单独的方法中:

    public class ExtractLen {
    private static final Pattern LEN_PATTERN = Pattern.compile(".*len (\\d*).*");
    
    public String extractLen(String s) {
        Matcher m1 = LEN_PATTERN.matcher(s);
        if (m1.matches()) {
            return m1.group(1);
        }
        return null;
    }}
    

    您可以将
    replaceFirst
    与不太简单的正则表达式一起使用:

    .*\blen\s+(\d+).*
    

    详细信息

    • *
      -除换行符以外的任何0+字符,尽可能多
    • \blen
      -一个完整的单词
      len
      \b
      是单词边界)
    • \s+
      -1+空格
    • (\d+)
      -第1组捕获1+个数字
    • *
      -除换行符以外的任何0+字符
    $1
    是对组1中捕获的内容的反向引用

    如果字符串可以包含换行符,请在正则表达式的开头添加
    (?s)

    见:


    这是一个正则表达式的候选。了解它。我没有清楚地表达自己。我只想要1-4位数字,不包括BBBTT如果你有
    111.112.111.0.0>111.33.44.55.11 pxs ikf len 3348 BBBTT18T
    ,你将得到
    334818
    而不是
    3348
    ,然后再次使用拆分s=s.split(“len”)[1]和s=s.split(“”[1];将从下一个空格中拆分。我是否可以直接在system.out.println中实现它作为一个条件,如子字符串?@cbll您可以使用更新的answer之类的方法将其提取到类中?我是否可以直接在system.out.println行中使用它?regexis@cbll您可以将其存储到变量中,也可以创建一个单独的函数然后让它只返回字符串。如果必须的话,我想你可以把它全部放在一行中,但这样做是不好的做法,而且很难阅读。
    .*\blen\s+(\d+).*
    
    String s  = "111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348";
    System.out.println(s.replaceFirst(".*\\blen\\s+(\\d+).*", "$1"));