0

I have an input string, let's say it's 111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348 , it might also sometimes be input via the stream as 111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348 BBBTTT.

How do I capture the numbers occuring after "len" in the via substring? Using system.out.println(stringStr.substring(stringStr.lastIndexOf(" ") + 1) will only print 3348 if it's the last part of the string above, but if BBBTTT appears, it will not work.

How do I print the numbers of a substring specifically after "len" but not including anything but the numbers that follow(might be 0-9999, not more), so not any characters following?

1
  • 2
    This is a candidate for RegularExpressions. Learn about it. Commented Oct 11, 2016 at 18:38

5 Answers 5

3

Use RegEx. The pattern would be something like "len ([\d]+)"

String inputString = "111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348 BBBTTT";
String regEx = "len ([\\d]+)";
String regExMatch = "";

Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(inputString);
if (m.find()) {
    regExMatch = m.group(1); // save the first capture group
}
System.out.println(regExMatch);
Sign up to request clarification or add additional context in comments.

2 Comments

Can't I use this directly in the system.out.println line? regex that is
@cbll you could either store it to a variable or create a separate function and have it return just the string. If you HAD to, I guess you could put it all in a single line, but it's not good practice to do so and it would be difficult to read.
2

You can use split method for this.

        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);

or just

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

3 Comments

I didnt express myself clearly. I want only the 1-4 digits, not including BBBTTT
still not good if you have 111.112.111.0.0 > 111.33.44.55.11 pxs ikf len 3348 BBBTT18T, you will get 334818 instead of 3348
then use split again s = s.split("len")[1]; and s = s.split(" ")[1]; will split from the next whitespace
1

You could be using regular expressions here, but there are other ways:

  1. Determine the index of "len" in your string
  2. Add 4 to that - that is where your number starts
  3. Do a substring from there

That string will either contain "just digigits"; or digits followed by a space. So you can do another indexOf() call to figure if there is a space; and if so, throw away everything after that space, too; like:

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

Hope that helps.

Comments

1

You may use a replaceFirst with a not-so-simple regex:

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

See the regex demo

Details:

  • .* - any 0+ chars other than linebreak symbols, as many as possible
  • \blen - a whole word len (\b is a word boundary)
  • \s+ - 1+ whitespaces
  • (\d+) - Group 1 capturing 1+ digits
  • .* - any 0+ chars other than linebreak symbols.

The $1 is a backereference to the contents captured into Group 1.

If your string can contain linebreaks, add (?s) at the start of the regex.

See Java demo:

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"));

Comments

1

Example with your two strings using a regex :

        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));
        }

You could put this in a separate method :

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;
}}

2 Comments

Can't I implement it directly in the system.out.println as a condition, like the substring?
@cbll You could extract it to a class with method like the updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.