具有以下输入字符串:
"health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open first_index 5 1 222173 0 43.8gb 43.8gb
green open second_index 5 1 27131 7 36.6gb 36.6gb
red open third_index 5 1 4047 0 22.4mb 22.4mb
"
如何获取以下输出字符串,该字符串采用第一列health
和第三列index
?
"first_index - yellow, first_index - green, third_index - red"
提前致谢。
PS:index
名称可能有所不同,没有_index
。上面的示例都有_index
但是可以有没有任何_index
的索引。 status
的值也会有所不同。
答案 0 :(得分:1)
除其他外,这将有效:
^(\w+)\W+\w+\W+(\w+)
选择小组\2
和\1
,查看a demo on regex101.com(并注意MULTILINE
修饰符)。
PHP
代码(demo on ideone.com):
<?php
$string = <<<DATA
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open first_index 5 1 222173 0 43.8gb 43.8gb
green open second_index 5 1 27131 7 36.6gb 36.6gb
red open third_index 5 1 4047 0 22.4mb 22.4mb
DATA;
$regex = '~^(\w+)\W+\w+\W+(\w+)~m';
preg_match_all($regex, $string, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
echo $match[2] . "-" . $match[1] . "\n";
}
?>
答案 1 :(得分:1)
答案 2 :(得分:1)
这是一种完成工作的方法:
$str = "health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open first_index 5 1 222173 0 43.8gb 43.8gb
green open second_index 5 1 27131 7 36.6gb 36.6gb
red open third_index 5 1 4047 0 22.4mb 22.4mb
";
preg_match_all('/\R(\w+)\h+\w+\h+(\w+)/', $str, $m);
print_r($m);
<强>输出:强>
Array
(
[0] => Array
(
[0] =>
yellow open first_index
[1] =>
green open second_index
[2] =>
red open third_index
)
[1] => Array
(
[0] => yellow
[1] => green
[2] => red
)
[2] => Array
(
[0] => first_index
[1] => second_index
[2] => third_index
)
)