0

StringBuilder values outputs following values.

StringBuilder values = new StringBuilder();

2020 May 12 09:28:11.292856 arrisxi6 runAppManager.sh[28161]: 200512-09:28:11.277875 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 2015
2020 May 12 09:28:12.282703 arrisxi6 runAppManager.sh[28161]: 200512-09:28:12.281919 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 12025
2020 May 12 09:28:13.280853 arrisxi6 runAppManager.sh[28161]: 200512-09:28:13.280054 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 15029
2020 May 12 09:28:14.280813 arrisxi6 runAppManager.sh[28161]: 200512-09:28:14.280181 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 14029

From the above values I need to get values 2015, 12025, 15029, 14029 as list . It can be integer, long or double. Using regex How Can i get these values

2 Answers 2

3

You may try the below regex to achieve your purpose:

videoBufferedMiliseconds\s+(\d+(?:\.\d+)?)$

Explanation of the above regex:

videoBufferedMiliseconds\s+ - Matches videoBufferedMiliseconds literally along with one or more white-space characters.

(\d+(?:\.\d+)?) - Represents a capturing group capturing integers, double or long.

$ - Represents end of the line.

pictorial representation

You can find the demo of the above regex in here.

Sample Implementation in java:

import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main
{
    private static final Pattern pattern = Pattern.compile("videoBufferedMiliseconds\\s+(\\d+(?:\\.\\d+)?)$", Pattern.MULTILINE);
    public static void main(String[] args) {
        final String string = "2020 May 12 09:28:11.292856 arrisxi6 runAppManager.sh[28161]: 200512-09:28:11.277875 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 2015.234\n"
     + "2020 May 12 09:28:12.282703 arrisxi6 runAppManager.sh[28161]: 200512-09:28:12.281919 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 12025\n"
     + "2020 May 12 09:28:13.280853 arrisxi6 runAppManager.sh[28161]: 200512-09:28:13.280054 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 15029\n"
     + "2020 May 12 09:28:14.280813 arrisxi6 runAppManager.sh[28161]: 200512-09:28:14.280181 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 14029";
        
        Matcher matcher = pattern.matcher(string);
        while(matcher.find()){
            System.out.println(matcher.group(1));
        }
    }
}

You can find the sample run of the above implementation in here.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use positive lookbehind assertion for this

Pattern pattern = Pattern.compile("(?<=videoBufferedMiliseconds)\\s*(\\d+.?\\d+)");

Code:

StringBuilder values = new StringBuilder();
values.append("2020 May 12 09:28:11.292856 arrisxi6 runAppManager.sh[28161]: 200512-09:28:11.277875 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 2015\n")
        .append("2020 May 12 09:28:12.282703 arrisxi6 runAppManager.sh[28161]: 200512-09:28:12.281919 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 12025\n")
        .append("2020 May 12 09:28:13.280853 arrisxi6 runAppManager.sh[28161]: 200512-09:28:13.280054 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 15029\n")
        .append("2020 May 12 09:28:14.280813 arrisxi6 runAppManager.sh[28161]: 200512-09:28:14.280181 [mod=RDKBROWSER2, lvl=INFO] [tid=28787] onConsoleLog:rdkbrowser.cpp:1130 [ConsoleAPI:473]: Progress: videoBufferedMiliseconds 14029.123\n");

Pattern pattern = Pattern.compile("(?<=videoBufferedMiliseconds)\\s*(\\d+.?\\d+)");
Matcher matcher = pattern.matcher(values);
while (matcher.find()) {
    System.out.println(matcher.group());
}

Output:

2015
12025
15029
14029.123

2 Comments

This answer is also good +1. Just will recommend adding the case for decimal values too. Your regex will not match for decimal values as mentioned in the requirement by the OP. Please check here. Please edit and it will be fine.
oh yes, that's a good point! thanks:) updated the 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.