正则表达式匹配特殊字符串和新行

时间:2014-12-24 11:55:43

标签: java regex

!54993A3D.0000!========================>  XXX Service Log  <========================
+54993A3D.0000      System Name: XXXXXXXXXXX                 Process ID: 1404
+54993A3D.0000     Program Name: XXXXXXXXXXX               User Name: SYSTEM
+54993A3D.0000        Task Name: XXXXXXXXXXX             System Type: Win2008;6.0-SP2
+54993A3D.0000   MAC1_ENV Macro: 0xC112                      Start Date: 2014/12/23
+54993A3D.0000       Start Time: 15:17:41                     CPU Count: 2
+54993A3D.0000        Page Size: 4K                         Phys Memory: 4095M
+54993A3D.0000      Virt Memory: 2048M                       Page Space: 8413M
+54993A3D.0000   UTC Start Time: 54993a3d                      ITM Home: C:\XXXXXXXXXXX\test2.exeX
+54993A3D.0000      EEM Process: esx9-v52_vm      Effective User Name: SYSTEM
+54993A3D.0000    Service Point: system.esx9-v52_vm
+54993A3D.0000  Executable Name: C:\XXXXXXXXXXX\test2.exe
+54993A3D.0000         KBB_RAS1: ERROR
+54993A3D.0000     KBB_RDFBB_ENS: C:\XXXXXXXXXXX\test2.exe
+54993A3D.0000 ================================================================================
(54993A3D.0000-1450:RDAS1,630,"CTBLD") 
+54993A3D.0000    Component: kbb
+54993A3D.0000       Driver: tms_ctbs630:d2325a/4288607.1
+54993A3D.0000    Timestamp: Nov 20 2012 20:21:28

我想在

之间提取数据
!54993A3D.0000!========================>  XXX Service Log  <========================

+54993A3D.0000 =======================================================================

使用java或regex

我尝试使用下面的代码,它可以解决问题

String pattern1 = "^.*?========================>  XXX Service Log  <========================";
      String pattern2 = "^.*?================================================================================";
      Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2),Pattern.DOTALL);
      Matcher m = p.matcher(text);
      while (m.find()) {
        System.out.println(m.group(1));
      }

2 个答案:

答案 0 :(得分:2)

![0-9a-zA-Z.]+!=+>[a-zA-Z\s]+<=+([\s\S]*?)(?=\+[0-9a-zA-Z.\s]+=+)

试试这个。抓住捕获或组1。参见演示。

https://regex101.com/r/gQ3kS4/37

答案 1 :(得分:1)

从正则表达式中删除锚点并按如下所示进行修改。

String text = "!54993A3D.0000!========================>  XXX Service Log  <========================\n" + 
            "+54993A3D.0000      System Name: XXXXXXXXXXX                 Process ID: 1404\n" + 
            "+54993A3D.0000     Program Name: XXXXXXXXXXX               User Name: SYSTEM\n" + 
            "+54993A3D.0000        Task Name: XXXXXXXXXXX             System Type: Win2008;6.0-SP2\n" + 
            "+54993A3D.0000   MAC1_ENV Macro: 0xC112                      Start Date: 2014/12/23\n" + 
            "+54993A3D.0000       Start Time: 15:17:41                     CPU Count: 2\n" + 
            "+54993A3D.0000        Page Size: 4K                         Phys Memory: 4095M\n" + 
            "+54993A3D.0000      Virt Memory: 2048M                       Page Space: 8413M\n" + 
            "+54993A3D.0000   UTC Start Time: 54993a3d                      ITM Home: C:\\XXXXXXXXXXX\\test2.exeX\n" + 
            "+54993A3D.0000      EEM Process: esx9-v52_vm      Effective User Name: SYSTEM\n" + 
            "+54993A3D.0000    Service Point: system.esx9-v52_vm\n" + 
            "+54993A3D.0000  Executable Name: C:\\XXXXXXXXXXX\\test2.exe\n" + 
            "+54993A3D.0000         KBB_RAS1: ERROR\n" + 
            "+54993A3D.0000     KBB_RDFBB_ENS: C:\\XXXXXXXXXXX\\test2.exe\n" + 
            "+54993A3D.0000 ================================================================================\n" + 
            "(54993A3D.0000-1450:RDAS1,630,\"CTBLD\") \n" + 
            "+54993A3D.0000    Component: kbb\n" + 
            "+54993A3D.0000       Driver: tms_ctbs630:d2325a/4288607.1\n" + 
            "+54993A3D.0000    Timestamp: Nov 20 2012 20:21:28";
    String pattern1 = "[^\n]*========================>  XXX Service Log  <========================\n";
    String pattern2 = "\n[^\n]*================================================================================";
    Pattern p = Pattern.compile(pattern1 + "(.*?)" + pattern2,Pattern.DOTALL);
    Matcher m = p.matcher(text);
    while (m.find()) {
      System.out.println(m.group(1));
    }

输出:

+54993A3D.0000      System Name: XXXXXXXXXXX                 Process ID: 1404
+54993A3D.0000     Program Name: XXXXXXXXXXX               User Name: SYSTEM
+54993A3D.0000        Task Name: XXXXXXXXXXX             System Type: Win2008;6.0-SP2
+54993A3D.0000   MAC1_ENV Macro: 0xC112                      Start Date: 2014/12/23
+54993A3D.0000       Start Time: 15:17:41                     CPU Count: 2
+54993A3D.0000        Page Size: 4K                         Phys Memory: 4095M
+54993A3D.0000      Virt Memory: 2048M                       Page Space: 8413M
+54993A3D.0000   UTC Start Time: 54993a3d                      ITM Home: C:\XXXXXXXXXXX\test2.exeX
+54993A3D.0000      EEM Process: esx9-v52_vm      Effective User Name: SYSTEM
+54993A3D.0000    Service Point: system.esx9-v52_vm
+54993A3D.0000  Executable Name: C:\XXXXXXXXXXX\test2.exe
+54993A3D.0000         KBB_RAS1: ERROR
+54993A3D.0000     KBB_RDFBB_ENS: C:\XXXXXXXXXXX\test2.exe

DEMO