在RegEX中的2个时间戳模式之间提取日志消息

我有下面的日志文件,正在使用NodeJS写日志监视。 我的parsing器是基于正则expression式,所以如果我收到的日志文件中可以被翻译成多个日志消息的新行,我应该能够提取它们。 为此,我需要一个rtegex来提取可变日志消息

Fri Jan 24 05:28:57 2014 MEMORY_TARGET defaulting to 1128267776. * instance_number obtained from CSS = 1, checking for the existence of node 0... * node 0 does not exist. instance_number = 1 Starting ORACLE instance (normal) LICENSE_MAX_SESSION = 0 LICENSE_SESSIONS_WARNING = 0 Initial number of CPU is 48 Number of processor cores in the system is 24 Number of processor sockets in the system is 12 Private Interface 'nxge20:1' configured from GPnP for use as a private interconnect. abc [name='nxge20:1', type=1, ip=169.254.121.29, mac=00-21-28-0e-8c-ae-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=169.254.0.0/16, mask=255.255.0.0, use=haip:cluster_interconnect/62] Public Interface 'nxge0' configured from GPnP for use as a public interface. [name='nxge0', type=1, ip=172.20.70.18, mac=00-21-28-0e-94-ce-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=172.20.70.0/24, mask=255.255.255.0, use=public/1] Public Interface 'nxge21' configured from GPnP for use as a public interface. [name='nxge21', type=1, ip=100.100.100.1, mac=00-21-28-0e-8c-af-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=100.100.100.0/23, mask=255.255.254.0, use=public/1] Shared memory segment for instance monitoring created CELL communication is configured to use 0 interface(s): CELL IP affinity details: NUMA status: NUMA system w/ 3 process groups cellaffinity.ora status: cannot find affinity map at '/etc/oracle/cell/network-config/cellaffinity.ora' (see trace file for details) CELL communication will use 1 IP group(s): Grp 0: Picked latch-free SCN scheme 3 Using LOG_ARCHIVE_DEST_1 parameter default value as /dbtop/grid/dbs/arch Autotune of undo retention is turned on. LICENSE_MAX_USERS = 0 SYS auditing is disabled NOTE: Volume support enabled NUMA system with 3 nodes detected Starting up: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Real Application Clusters and Automatic Storage Management options. ORACLE_HOME = /dbtop/grid System name: SunOS Node name: sec33-e Release: 5.10 Version: Generic_142909-17 Machine: sun4u Using parameter settings in server-side spfile /dbtop/asm/dbs/spfile+ASM.ora System parameters with non-default values: large_pool_size = 12M instance_type = "asm" remote_login_passwordfile= "EXCLUSIVE" asm_diskstring = "/dev/rdsk/*" asm_diskgroups = "WFREC" asm_diskgroups = "WFDATA" asm_power_limit = 7 diagnostic_dest = "/dbtop/app/oracle" Cluster communication is configured to use the following interface(s) for this instance 169.254.121.29 cluster interconnect IPC version:Oracle UDP/IP (generic) IPC Vendor 1 proto 2 Fri Jan 24 05:29:03 2014 PMON started with pid=2, OS id=17520 Fri Jan 24 05:29:04 2014 PSP0 started with pid=3, OS id=17521 Fri Jan 24 05:29:05 2014 VKTM started with pid=4, OS id=17522 at elevated priority VKTM running at (10)millisec precision with DBRM quantum (100)ms 

我试图匹配的数据时间和地点在一个组使用下面的正则expression式:

 (\w{1,3} \w{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}) 

这是匹配的时间戳,我怎么能抓住第二组中的其他日志消息

你可以使用

 /^([az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})\r?\n(.*(?:\r?\n(?![az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*)/igm 

看正则expression式演示 。

该模式包括您的模式和一个额外的部分,匹配到该模式或文本的结尾。

  • ^ – 行的开始
  • ([az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}) – 组1匹配时间戳
  • \r?\n – 换行符
  • (.*(?:\r?\n(?![az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*) – 组2捕获0+序列:
    • .* – 任何0+字符尽可能多的行中断字符
    • (?:\r?\n(?![az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)* – 零次或多次出现次数:
      • \r?\n(?![az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}) – 时间戳记模式后面没有的换行符
      • .* – 任何0+字符尽可能多的行中断字符。

JS演示:

 var rx = /^([az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4})\r?\n(.*(?:\r?\n(?![az]{1,3} [az]{1,3} \d{1,2} \d{1,2}:\d{1,2}:\d{1,2} \d{4}).*)*)/gmi; var str = "Fri Jan 24 05:28:57 2014\r\nMEMORY_TARGET defaulting to 1128267776.\r\n* instance_number obtained from CSS = 1, checking for the existence of node 0... \r\n* node 0 does not exist. instance_number = 1 \r\nStarting ORACLE instance (normal)\r\nLICENSE_MAX_SESSION = 0\r\nLICENSE_SESSIONS_WARNING = 0\r\nInitial number of CPU is 48\r\nNumber of processor cores in the system is 24\r\nNumber of processor sockets in the system is 12\r\nPrivate Interface 'nxge20:1' configured from GPnP for use as a private interconnect.\r\nabc\r\n [name='nxge20:1', type=1, ip=169.254.121.29, mac=00-21-28-0e-8c-ae-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=169.254.0.0/16, mask=255.255.0.0, use=haip:cluster_interconnect/62]\r\nPublic Interface 'nxge0' configured from GPnP for use as a public interface.\r\n [name='nxge0', type=1, ip=172.20.70.18, mac=00-21-28-0e-94-ce-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=172.20.70.0/24, mask=255.255.255.0, use=public/1]\r\nPublic Interface 'nxge21' configured from GPnP for use as a public interface.\r\n [name='nxge21', type=1, ip=100.100.100.1, mac=00-21-28-0e-8c-af-00-00-00-00-00-00-00-00-00-00-00-2f-ff-ff, net=100.100.100.0/23, mask=255.255.254.0, use=public/1]\r\nShared memory segment for instance monitoring created\r\nCELL communication is configured to use 0 interface(s):\r\nCELL IP affinity details:\r\n NUMA status: NUMA system w/ 3 process groups\r\n cellaffinity.ora status: cannot find affinity map at '/etc/oracle/cell/network-config/cellaffinity.ora' (see trace file for details)\r\nCELL communication will use 1 IP group(s):\r\n Grp 0: \r\nPicked latch-free SCN scheme 3\r\nUsing LOG_ARCHIVE_DEST_1 parameter default value as /dbtop/grid/dbs/arch\r\nAutotune of undo retention is turned on. \r\nLICENSE_MAX_USERS = 0\r\nSYS auditing is disabled\r\nNOTE: Volume support enabled\r\nNUMA system with 3 nodes detected\r\nStarting up:\r\nOracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production\r\nWith the Real Application Clusters and Automatic Storage Management options.\r\nORACLE_HOME = /dbtop/grid\r\nSystem name: SunOS\r\nNode name: sec33-e\r\nRelease: 5.10\r\nVersion: Generic_142909-17\r\nMachine: sun4u\r\nUsing parameter settings in server-side spfile /dbtop/asm/dbs/spfile+ASM.ora\r\nSystem parameters with non-default values:\r\n large_pool_size = 12M\r\n instance_type = \"asm\"\r\n remote_login_passwordfile= \"EXCLUSIVE\"\r\n asm_diskstring = \"/dev/rdsk/*\"\r\n asm_diskgroups = \"WFREC\"\r\n asm_diskgroups = \"WFDATA\"\r\n asm_power_limit = 7\r\n diagnostic_dest = \"/dbtop/app/oracle\"\r\nCluster communication is configured to use the following interface(s) for this instance\r\n 169.254.121.29\r\ncluster interconnect IPC version:Oracle UDP/IP (generic)\r\nIPC Vendor 1 proto 2\r\nFri Jan 24 05:29:03 2014\r\nPMON started with pid=2, OS id=17520 \r\nFri Jan 24 05:29:04 2014\r\nPSP0 started with pid=3, OS id=17521 \r\nFri Jan 24 05:29:05 2014\r\nVKTM started with pid=4, OS id=17522 at elevated priority\r\nVKTM running at (10)millisec precision with DBRM quantum (100)ms"; var m, res = []; while ((m = rx.exec(str)) !== null) { res.push([m[1], m[2]]); } console.log(res);