Home > Net >  Java regular expression to get submetric from logfiles
Java regular expression to get submetric from logfiles

Time:10-27

I'm trying to get submetrics from logs with thousands of lines. Those lines are not similar so i need to match some values before get submetric.

Some simplified example lines

1, 2, acceptline,x,111, 100, , , 20, 20, end
1, 2, declineline,x,x, 100, 20, , end
1, 2, 3, acceptline,x,x, 1000, , 40, end

I try to get that numeric value which is in third comma separated column after my value match (acceptline).

In my example those values are 100 and 1000 but those can basically be any numeric value

I have succeeded to got that right submetric value with following java regular expression

^.*acceptline. ?((?<submetric>,. ?){3}),.*

But within that regex i got submetric as <, 100> or what ever that numeric value is. Now i need to improve that regex so those leading <, > is removed before accepted as submetric.

CodePudding user response:

You can use

\bacceptline(?:,[^,]*){2},\s*(\d )

If the number can be a float one, use \d*\.?\d instead of \d .

See the regex demo. Details:

  • \b - a word boundary
  • acceptline - a word
  • (?:,[^,]*){2} - two occurrences of a comma and then zero or more non-commas
  • , - a comma
  • \s* - zero or more whitespaces
  • (\d ) - Group 1: one or more digits.

See a Java demo:

String string = "1, 2, acceptline,x,111, 100, , , 20, 20, end\n1, 2, declineline,x,x, 100, 20, , end\n1, 2, 3, acceptline,x,x, 1000, , 40, end";
        
String regex = "\\bacceptline(?:,[^,]*){2},\\s*(\\d )";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
        
while (matcher.find()) {
    System.out.println(matcher.group(1));
}
// => 100 and 1000

CodePudding user response:

Regarding regex101.com I finally got that right.

^.*acceptline. ?((,[^\,]*){1}),\s*(?<submetric>\d ).*

Thanx for Wiktor to pushing me right way with those \s and \d switches.

  • Related