Sourabh
Sourabh

Reputation: 79

How to write a regex for the following strings?

In the given strings I just want to extract strings after "ENERGY:"

1562173405047|INFO|MyHalfDuplexModem@30:println|ENERGY: -s 1 -d 2 -b 288 -e 9.999788799994675071125   T   {-s 1, -d 2 }
1562173405552|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 3 -d 2 -b 96 -e 9.999779196127731100294   R   {-s 3, -d 2 }
1562173406122|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 1 -d 2 -b 288 -e 9.999764796127731100294   R   {-s 1, -d 2 }
1562173406194|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 2 -d 1 -b 96 -e 9.999759995924876667052   T   {-s 2, -d 1 }

this is my code :

public static void main(String[] args) {
    //movies = new ArrayList<movie>();
       realPath = "Z:\\UNET\\3 NODE 1 SOURCE\\log-0.txt";
       File f = new File(realPath);
       if ( !f.exists()) {
          System.err.println("file path not specified");
       }
       try {
        String regex1 = "[0-9]+\|INFO\|MyHalfDuplexModem@[0-9]+:println\|ENERGY:";

        Scanner sc = new Scanner(f);

            while (sc.hasNextLine()) {

                String nextLine = sc.nextLine();
                if ( !nextLine.matches(regex1)) {
                     System.out.println(nextLine);
                }


            }   

         //   sc.close();
         } catch(Exception e) {
              throw new RuntimeException(e);
         }
       }

I tried the following regex but it's not working : regex1 = "[0-9]+[|]INFO[|]MyHalfDuplexModem@[0-9]+:println|ENERGY:";

Upvotes: 0

Views: 50

Answers (2)

Mansur
Mansur

Reputation: 1829

I think, this one captures what you want. It is not in Java, it's pure regex.

ENERGY:\s(.*)

Here's the Java example

public static void main(String[] args) {
    Pattern p = Pattern.compile("ENERGY:\\s(.*)");
    String st = "1562173405047|INFO|MyHalfDuplexModem@30:println|ENERGY: -s 1 -d 2 -b 288 -e 9.999788799994675071125   T   {-s 1, -d 2 }\n" +
            "1562173405552|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 3 -d 2 -b 96 -e 9.999779196127731100294   R   {-s 3, -d 2 }\n" +
            "1562173406122|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 1 -d 2 -b 288 -e 9.999764796127731100294   R   {-s 1, -d 2 }\n" +
            "1562173406194|INFO|MyHalfDuplexModem@43:println|ENERGY: -s 2 -d 1 -b 96 -e 9.999759995924876667052   T   {-s 2, -d 1 }";
    Matcher mt = p.matcher(st);

    while (mt.find()) {
        String group = mt.group(1);
        System.out.println(group);
    }
}

Output:

-s 1 -d 2 -b 288 -e 9.999788799994675071125   T   {-s 1, -d 2 }
-s 3 -d 2 -b 96 -e 9.999779196127731100294   R   {-s 3, -d 2 }
-s 1 -d 2 -b 288 -e 9.999764796127731100294   R   {-s 1, -d 2 }
-s 2 -d 1 -b 96 -e 9.999759995924876667052   T   {-s 2, -d 1 }

Upvotes: 0

jensgram
jensgram

Reputation: 31508

With a bit of escaping you should be good:

regex1 = "[0-9]+\|INFO\|MyHalfDuplexModem@[0-9]+:println\|ENERGY:";
                                                        ^^ this was the problem
                                                        |
                                                        `- this is the fix

Without the escape | is an alternation, effectively resulting in "[0-9]+|INFO|MyHalfDuplexModem@[0-9]+:println" OR "ENERGY:"

Also notice that \| is identical to [|]. I prefer the former.

Upvotes: 2

Related Questions