Raman
Raman

Reputation: 717

Specific search pattern RegExp

I am new to RegExp, but did some basic tutorial. From the below, I need to be able extract the following. Your help with this would enhance my understanding with RegExp.

Extract String:

2016-02-17 19:59:18,182 GMT [transaction-Id=47ad8e96-1db8-4b41-85e5-9256fca485ab] [request_uri=/mydevice/2232234] [] INFO Response:{"executionTimeInMillis":54,"transactionId":"3191569800","success":false,"internalTransactionId":"47ad8e96-1db8-4b41-85e5-9256fca485ab","operationResults":[{"operation":"addDevice","code":"409","message":"Cannot add device"}]}

Output string value to be extracted:

  1. 47ad8e96-1db8-4b41-85e5-9256fca485ab
  2. 22xs32234
  3. {"operation":"addDevice","code":"409","message":"Cannot add device"}

Upvotes: 0

Views: 80

Answers (3)

zolo
zolo

Reputation: 469

(?<=[\/=\[])([^\/=\[]+)(?=\]) if you would like to refer to the match, (?<=[\/=\[])[^\/=\[]+(?=\]) if you don't.

Is the "22xs32234" expected result is right? Since "2232234" is the original string. If yes, then these expressions are not ready for that yet.

DEMO

Another solution with fewer running step (faster, but figured out it gives back the whole URI which you didn't want): (?<=transaction-Id=|request_uri=|operationResults.:\[)[^\]]+

DEMO

Finally a fixed version of the last one (still faster a bit than first one): (?<=transaction-Id=|request_uri=|operationResults.:\[)(?:\/[^\]]+\/)?([^\]]+)

DEMO

Upvotes: 0

Sandeep
Sandeep

Reputation: 51

Perl Code:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $string = '2016-02-17 19:59:18,182 GMT [transaction-Id=47ad8e96-1db8-    4b41-85e5-9256fca485ab] [request_uri=/mydevice/2232234] [] INFO Response:{"executionTimeInMillis":54,"transactionId":"3191569800","success":false,"internalTransactionId":"47ad8e96-1db8-4b41-85e5-9256fca485ab","operationResults":[{"operation":"addDevice","code":"409","message":"Cannot add device"}]}';


    if($string =~ /(?:transaction\-Id=)(.*)\] \[(?:request_uri=\/mydevice\/)(.*)\] \[.*(?:operationResults.:\[)(.*)\]\}$/ig){
       print "$1\n$2\n$3\n";
    }
    else{
        print "no match\n";
    }

Upvotes: 1

zhanzezhu
zhanzezhu

Reputation: 121

transaction-Id=(.*?)].*?request_uri=(.*?)].*?"operationResults":[(.*?)]

extract $0,$1,$2

Upvotes: 1

Related Questions