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+