特定搜索模式RegExp

时间:2016-02-18 06:05:04

标签: regex

我是RegExp的新手,但做了一些基础教程。从下面,我需要能够提取以下内容。您对此的帮助将增强我对RegExp的理解。

提取字符串:

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"}]}

要提取的输出字符串值:

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

3 个答案:

答案 0 :(得分:1)

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

提取$ 0,$ 1,$ 2

答案 1 :(得分:1)

Perl代码:

    #!/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";
    }

答案 2 :(得分:0)

(?<=[\/=\[])([^\/=\[]+)(?=\])如果你想参考比赛,(?<=[\/=\[])[^\/=\[]+(?=\])如果不这样做。

“22xs32234”预期结果是否正确?由于“2232234”是原始字符串。如果是,那么这些表达式还没有准备好。

DEMO

另一个运行步骤较少的解决方案(更快,但想通了它会返回你不想要的整个URI): (?<=transaction-Id=|request_uri=|operationResults.:\[)[^\]]+

DEMO

最后一个固定版本(比第一个更快): (?<=transaction-Id=|request_uri=|operationResults.:\[)(?:\/[^\]]+\/)?([^\]]+)

DEMO