简体   繁体   English

似乎无法获得正确的PHP正则表达式量词

[英]can't seem to get right php regex quantifier

I have a string that looks like this: 我有一个看起来像这样的字符串:

msg": "log domain jl.lab.test.net server lab-jl-ppr-web"

I'm trying to extract "jl.lab.test.net" and JUST "lab-jl-ppr" from "lab-jl-ppr-web" using the following regular expression: 我正在尝试使用以下正则表达式从“ lab-jl-ppr-web”中提取“ jl.lab.test.net”和“ Jlab-jl-ppr”:

 preg_match("/\"msg\"\: \"log domain\s([\w*\.]*) server ([\w*\.\-]*)/i",$line,$matches);

The second group currently matches the entire "lab-jl-ppr-web" string. 第二组当前匹配整个“ lab-jl-ppr-web”字符串。 I have been trying to specify the proper quantifier but so far I haven't gotten the right one. 我一直在尝试指定适当的量词,但到目前为止,我还没有找到正确的量词。 I've tried the following: 我尝试了以下方法:

 preg_match("/\"msg\"\: \"log domain\s([\w*\.]*) server ([\w*\.\-]*){3}/i",$line,$matches);

I'm continuing to play with it but if you have any tips, i'd appreciate it. 我将继续使用它,但是如果您有任何提示,我将不胜感激。 Thanks. 谢谢。

Why not just 为什么不只是

/..snip.. server ([\w*\.\-]*)-web/i

? Just keep -web outside of the capture group. 只需将-web保留在捕获组之外。

This probably works 这可能有效

'~"msg":[ ]"log[ ]domain\s([\w.]*)[ ]server[ ]((?:(?!-web)[\w.-])*)~'  

but, it's hard to get what you're looking for from the regex. 但是,很难从正则表达式中获得所需的内容。

Expanded 展开式

 "msg": [ ] "log [ ] domain \s 
 ( [\w.]* )                    # (1)
 [ ] server [ ] 
 (                             # (2 start)
      (?:
           (?! -web )
           [\w.-] 
      )*
 )                             # (2 end)

Output 输出量

 **  Grp 0 -  ( pos 0 , len 52 ) 
"msg": "log domain jl.lab.test.net server lab-jl-ppr  
 **  Grp 1 -  ( pos 19 , len 15 ) 
jl.lab.test.net  
 **  Grp 2 -  ( pos 42 , len 10 ) 
lab-jl-ppr  

That one seems legit but i'm sure better one can be written. 那似乎是合法的,但我相信可以写得更好。

^log domain ([a-zA-Z\.]+) server ([a-zA-Z\.\-]+)-web$

Here you can test it 在这里可以测试

LiveRegex LiveRegex

Use the following approach with preg_match_all function: preg_match_all函数使用以下方法:

$str = "log domain jl.lab.test.net server lab-jl-ppr-web";
preg_match_all("/\b\w+\.\w+\.\w+\.\w+\b|\b\w+-\w+-\w+(?=-\w+?)\b/U", $str, $matches);

print_r($matches[0]);

The output: 输出:

Array
(
    [0] => jl.lab.test.net
    [1] => lab-jl-ppr
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM