Search code examples
javaregexregex-lookaroundsregex-groupregex-greedy

RegEx for capturing particular digits


From the below logs how can i grep '951792' values alone

2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   

I tried with java split/substring operations. but line of code is high. using regex how can i get '951792' values

The output will be

951792
951895
951934 
075041

Solution

  • Here, we might want to simply use the right boundary [mod next to our desired digits and collect the digits in our first capturing group, maybe similar to this:

    ([0-9]+)\s\[m 
    

    If we wish, we can add more boundaries, such as:

    (.+?)([0-9]+)\s\[m.+
    

    enter image description here

    DEMO

    Test

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    final String regex = "(.+?)([0-9]+)\\s\\[m.+";
    final String string = "2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes\n"
         + "2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e\n"
         + "2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   \n";
    final String subst = "\\2";
    
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);
    
    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);
    
    System.out.println("Substitution result: " + result);
    

    Demo

    const regex = /(.+?)([0-9]+)\s\[m.+/gm;
    const str = `2019 May 22 03:32:17.952296 france1v4 sh[4937]: 190522-03:32:17.951792 [mod=REC, lvl=INFO] [tid=26130] Recording A8602096210405800406L200218680503121519 size is 4145956224 bytes
    2019 May 22 03:32:17.952387 france1v4 sh[4937]: 190522-03:32:17.951895 [mod=REC, lvl=INFO] [tid=26130] RecordingInfo = fffocap://0x401e
    2019 May 22 03:32:17.952466 france1v4 sh[4937]: 190522-03:32:17.951934 [mod=REC, lvl=INFO] [tid=26130] recording_dvr_from_recording_info:physicalSegmentCount=10   
    `;
    const subst = `$2`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);

    RegEx

    If this expression wasn't desired, it can be modified or changed in regex101.com.

    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here