Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式模式来区分这些文本_Php_Regex_Preg Match - Fatal编程技术网

Php 正则表达式模式来区分这些文本

Php 正则表达式模式来区分这些文本,php,regex,preg-match,Php,Regex,Preg Match,我有下面的文本,我想要一个正则表达式模式: qos policy 1024SharedUnlimitedUserRX pwfq rate maximum 1792 weight 16 num-queues 4 queue-map Fastweb congestion-map Fastweb queue 0 priority 0 weight 100 queue 1 priority 1 weight 90 queue 2 priority 2 weight 70 queue 3 priorit

我有下面的文本,我想要一个正则表达式模式:

qos policy 1024SharedUnlimitedUserRX pwfq 
rate maximum 1792
weight 16
num-queues 4
queue-map Fastweb
congestion-map Fastweb
queue 0 priority 0 weight 100
queue 1 priority 1 weight 90
queue 2 priority 2 weight 70
queue 3 priority 3 weight 85
模式应该在第一个
权重
短语之后获取整数值。换句话说,我想要的是16的值,而不是接下来4行(100、90、70、85)的值

我写了这个模式:

/weight (\d*)/
但这个模式也发现了其他行的价值,我应该怎么做

注意:我使用了
preg\u math()
函数,而不是
preg\u match\u all()
这样做

  <?php
    $input_line="qos policy 1024SharedUnlimitedUserRX pwfq 
    rate maximum 1792
    weight 16
    num-queues 4
    queue-map Fastweb
    congestion-map Fastweb
    queue 0 priority 0 weight 100
    queue 1 priority 1 weight 90
    queue 2 priority 2 weight 70
    queue 3 priority 3 weight 85";

preg_match("/(?:weight\s(\d+)(?!\S+))/", $input_line, $output_array);
print_r($output_array);

你可以使用这个正则表达式

(?<=weight )\d+

链接:

此正则表达式可能适合您:

preg_match('/^weight (\d+)/m', $input, $match);

^
确保
重量
位于行的开头

m
标志使
^
在每行的开头匹配,而不仅仅是在字符串的开头

如果行的开头可能有您不想要的其他
重量
,您可以尝试此方法,以确保
重量
刚好在
最大值之后:

preg_match('/maximum \S*\s*weight (\d+)/m', $input, $match);

回答得好,因为你要解释它,而不是直接说出来。
Array
(
    [0] => 16
)
preg_match('/^weight (\d+)/m', $input, $match);
preg_match('/maximum \S*\s*weight (\d+)/m', $input, $match);
rate maximum \d+[\s\S]+?weight \d+