簡體   English   中英

正則表達式-分組配對下一場比賽

[英]Regex - Match up to next match in groups

我有以下要使用正則表達式拆分的字符串:

Connect: Unable to open connection to server. HOST:(servernamex) SERVICE:(cnxadaB0) DATABASEINFO:(3 255 C:\\DATABASENAME.DAT) ERROR:RemoteConnect: SocketError(10061) The CONNX Listener process (CNXRUN##_MAIN) is not running on the system.

我到目前為止想出的正則表達式如下:

(\s|^)([A-Z]\w\w+:(?!(\s[A-Z]\w\w+:)))

當我運行正則表達式時,我得到以下結果:

 MATCH 1 1. '' 2. 'Connect:' MATCH 2 1. ' ' 2. 'HOST:' MATCH 3 1. ' ' 2. 'SERVICE:' MATCH 4 1. ' ' 2. 'DATABASEINFO:' MATCH 5 1. ' ' 2. 'ERROR:' 

我想使用正則表達式將其拆分為如下所示的數據分組:

 MATCH 1 1. '' 2. 'Connect:' 3. 'Unable to open connection to server.' MATCH 2 1. ' ' 2. 'HOST:' 3. '(servernamex)' MATCH 3 1. ' ' 2. 'SERVICE:' 3. '(cnxadaB0)' MATCH 4 1. ' ' 2. 'DATABASEINFO:' 3. '(3 255 C:\\DATABASENAME.DAT)' MATCH 5 1. ' ' 2. 'ERROR:' 3. 'RemoteConnect: SocketError(10061) The CONNX Listener process (CNXRUN##_MAIN) is not running on the system. 

我玩過的游戲樣本regex101演示

您可以使用此正則表達式:

(^|\s)([A-Z]\w+:)\s*(.*?(?=\s[A-Z]\w+:|$))

正則演示

如果要避免首先捕獲帶有空格或空字符串的組,請使用lookbehind斷言:

(?<=^|\s)([A-Z]\w+:)\s*(.*?(?=\s[A-Z]\w+:|$))
([^: ]+?:(?![^(]*\)))\s*(.*?)(?=\s+[^: ]*:(?![^(]*\))|$)

您可以使用this.see demo。代替split進行match和捕獲groups

https://regex101.com/r/gT6vU5/3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM