5 Replies - 175 Views - Last Post: 24 April 2019 - 11:55 AM Rate Topic: -----

#1 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 93
  • Joined: 29-September 08

Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 07:11 AM

I am terrible at trying to figure out this Regex! It's, like everyone else, doesn't make much since.
The string looks like this:
15:26:04,017 INFO [stdout] (Log4j2-xxxxxxxxx-1) 2019-04-23 15:26:03,945 [ http-xxxxxxxxx-4] [ STANDARD] [ ] [ xxxxxxxxx:8] (rnal.loaders.xxxxxxxxx) INFO xxxxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxx|1xxxxxxxxx4|Rest|SystemManagement|v2|importxxxxxxxxxd1070581a|xxxxxxxxxPSUYP7A - The import WORK- PxxxxxxxxxWARNING #xxxxxxxxx.591 GMT

The pattern looks like this when breaking the above up:
15:26:04
017
INFO 
stdout
Log4j2-xxxxxxxxx-1
2019-04-23 
15:26:03
http-xxxxxxxxx-4
STANDARD
[] 
[xxxxxxxxx:8] 
rnal.loaders.xxxxxxxxx
INFO
xxxxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxx
1xxxxxxxxx4
Rest
SystemManagement
v2
importxxxxxxxxxd1070581a
xxxxxxxxxPSUYP7A 
The import WORK- PxxxxxxxxxWARNING #xxxxxxxxx.591
GMT

Any help with this crazy pattern would be great! I've been told that using [.*?] would help with the X in brackets but it's still over my head and I don't know how to go about this the LONG LONG way around (.replace, .contains, .indexOf) but I know using Regex is much cleaner.

This post has been edited by StealthRT: 24 April 2019 - 07:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pattern and picking out needed values using Regex with Java

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14987
  • View blog
  • Posts: 59,843
  • Joined: 12-June 08

Re: Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 07:30 AM

That's not bad.. You have nice areas to match up against.

( ) and [ ] need to be escaped if they are not being used for grouping. You escape by throwing a \ before their use.


INFO (\[.*?\]) (\(.*?\)) 


yields:
group 1: [stdout]
group 2: (Log4j2-xxxxxxxxx-1)


Group one is:

\[ escape bracket
. match any character
* any number of times
? minimal matching (not greedy)
\] escape end bracket.


Similar for group 2, but with parentheses and not brackets.


https://regex101.com/
http://regexlib.com/...CookieSupport=1

That same thinking can be applied for the rest. Refine as you need.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7429
  • View blog
  • Posts: 15,399
  • Joined: 16-October 07

Re: Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 07:45 AM

I don't see any code...

Right, begin with a tester:
void test(final String pattern) {
    System.out.println("Pattern: " + pattern);
    final String msg = "15:26:04,017 INFO [stdout] (Log4j2-xxxxxxxxx-1) 2019-04-23 15:26:03,945 [ http-xxxxxxxxx-4] [ STANDARD] [ ] [ xxxxxxxxx:8] (rnal.loaders.xxxxxxxxx) INFO xxxxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxx|1xxxxxxxxx4|Rest|SystemManagement|v2|importxxxxxxxxxd1070581a|xxxxxxxxxPSUYP7A - The import WORK- PxxxxxxxxxWARNING #xxxxxxxxx.591 GMT";
    final Matcher m = Pattern.compile(pattern).matcher(msg);
    System.out.println(m.matches());
    if (m.matches()) {
        for(int i=1; i<=m.groupCount(); i++) {
            System.out.println(i + ": " + m.group(i));
        }
    }
    System.out.println("---");
}



Now start whacking. Whittle it down from the the front. First expression reads until a comma, etc. e.g.
test("");
test("^.*");
test("^([^,]*).*");
test("^([^,]*),(\\d+).*");



Results:
run:
Pattern: 
false
---
Pattern: ^.*
true
---
Pattern: ^([^,]*).*
true
1: 15:26:04
---
Pattern: ^([^,]*),(\d+).*
true
1: 15:26:04
2: 017
---



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 StealthRT   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 93
  • Joined: 29-September 08

Re: Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 09:48 AM

View Postbaavgai, on 24 April 2019 - 09:45 AM, said:

I don't see any code...

Right, begin with a tester:
void test(final String pattern) {
    System.out.println("Pattern: " + pattern);
    final String msg = "15:26:04,017 INFO [stdout] (Log4j2-xxxxxxxxx-1) 2019-04-23 15:26:03,945 [ http-xxxxxxxxx-4] [ STANDARD] [ ] [ xxxxxxxxx:8] (rnal.loaders.xxxxxxxxx) INFO xxxxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxx|1xxxxxxxxx4|Rest|SystemManagement|v2|importxxxxxxxxxd1070581a|xxxxxxxxxPSUYP7A - The import WORK- PxxxxxxxxxWARNING #xxxxxxxxx.591 GMT";
    final Matcher m = Pattern.compile(pattern).matcher(msg);
    System.out.println(m.matches());
    if (m.matches()) {
        for(int i=1; i<=m.groupCount(); i++) {
            System.out.println(i + ": " + m.group(i));
        }
    }
    System.out.println("---");
}



Now start whacking. Whittle it down from the the front. First expression reads until a comma, etc. e.g.
test("");
test("^.*");
test("^([^,]*).*");
test("^([^,]*),(\\d+).*");



Results:
run:
Pattern: 
false
---
Pattern: ^.*
true
---
Pattern: ^([^,]*).*
true
1: 15:26:04
---
Pattern: ^([^,]*),(\d+).*
true
1: 15:26:04
2: 017
---



Hope this helps.


Yeah even with your suggestion(s) I am unable to come up with even a charater being reconized.

I also noticed that the SSH log(s) are not all the same format:
12:30:58,818 INFO  [x] (x:x, x: [x], Partitions=[0,1,3]) lefd version : 0.11.0.3


So that above is different form:
15:26:04,017 INFO [stdout] (Log4j2-xxxxxxxxx-1) 2019-04-23 15:26:03,945 [ http-xxxxxxxxx-4] [ STANDARD] [ ] [ xxxxxxxxx:8] (rnal.loaders.xxxxxxxxx) INFO xxxxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxx|1xxxxxxxxx4|Rest|SystemManagement|v2|importxxxxxxxxxd1070581a|xxxxxxxxxPSUYP7A - The import WORK- PxxxxxxxxxWARNING #xxxxxxxxx.591 GMT


So in my opinion this is totally out of my realm of Regex knowledge - even if regex can split all of these up regardless of form.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7429
  • View blog
  • Posts: 15,399
  • Joined: 16-October 07

Re: Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 11:37 AM

If you have consistent patterns then you should generally be able match them with a regular expression. However, if your patterns are not consistent, then you're screwed regardless.

One approach would be to have a series of matching rules, most likely to least likely, and take the first hit.

Also, while doing all your parsing in a single regex is nice, it needn't be written in stone. You could take a first pass, get a few large chunks, and then apply different logic to those chunks. You have Java, a full blown general purpose language. If regex is confounding you, you needn't use it at all.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3623
  • View blog
  • Posts: 16,656
  • Joined: 20-September 08

Re: Pattern and picking out needed values using Regex with Java

Posted 24 April 2019 - 11:55 AM

Quote

Pattern and picking out needed values

So what exactly are the "needed values"?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1