我有一個包含以下內容的檔案文本:
define host {
use host-template
host_name server-01
display_name server-01-display-name
address 10.0.0.1
_CUSTOMEREMAIL [email protected]
_LBZ LBZ#10178541
}
define service {
use host-template
host_name server-02
service_description server-02
servicegroups wlan
display_name server-02
_LBZ LBZ#425964
_TARGETIP 10.0.0.2
_CUSTOMEREMAIL [email protected]
}
define service {
use host-template
host_name server-03
service_description server-03
servicegroups wlan
display_name server-04
_LBZ LBZ#421264
_TARGETIP 10.0.0.3
_CUSTOMEREMAIL [email protected]
}
define host {
use host-template
host_name server-04
display_name server-04
address 10.0.0.4
_CUSTOMEREMAIL [email protected]
_LBZ LBZ#12300541
}
任務: 必須通過引數“_LBZ”找到 server02 - 在本例中為 425964。一旦找到定義,它還應該捕獲引數“host_name”、“_LBZ”和“_CUSTOMEREMAIL”。
希望的結果:
- 服務器-02
- 425964
- [email protected]
你能幫我嗎?第二天我就帶著這個表情坐在這里。
uj5u.com熱心網友回復:
這是一種方法:
{(?=[^}]*?host_name\s*([^\s] ))(?=[^}]*?_LBZ\s*LBZ#(425964))(?=[^}]*?_TARGETIP\s*([^\s] ))
查看在線演示:
第一組將是host_name,第二組_LBZ和最后一組_CUSTOMEREMAIL。
{: 匹配{。(?=): 正面展望。[^}]*?}:盡可能少地匹配除 之外的任何字符,介于零次和無限次之間。host_name: 匹配host_name。\s*: 匹配任意數量的空格。(): 捕獲組[^\s]. 盡可能匹配除空格以外的任何字符,一次到無限次。
其他兩個前瞻的構造類似,以捕獲其他引數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444970.html
標籤:正则表达式
上一篇:匹配字串但忽略可選后綴?
