Having the following input string:
具有以下输入字符串:
"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
"
How can I get the following output string, which takes the first column, health
and the 3rd one, index
?
如何获得以下输出字符串,它取第一列health,第三列index?
"first_index - yellow, first_index - green, third_index - red"
Thanks in advance.
提前谢谢。
PS: The index
names can vary and have no _index
. The example above all of them have _index
but there can be indexes without any _index
. The values of status
can vary too.
索引名可以变化,并且没有_index。上面的示例都有_index,但是可以有没有任何_index的索引。地位的价值也可能不同。
3 个解决方案
#1
1
Among others, this will work:
除其他外,这将起作用:
^(\w+)\W+\w+\W+(\w+)
Take group \2
and \1
, see a demo on regex101.com (and mind the MULTILINE
modifier).
拿组\2和\1,看看regex101.com上的演示(注意多行修改器)。
As
PHP
code (
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";
}
?>
#2
1
You can use this regex with 2 captured groups in preg_match_all
function:
您可以在preg_match_all函数中使用这个regex和两个捕获的组:
/^(\S+)\h+\S+\h+(\S+)/m
You can then take capture group-2 and capture group-1 to format your output.
然后可以获取capture group-2和capture group-1来格式化输出。
RegEx演示
#3
1
Here is a way to do the job:
下面是一种方法:
$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);
Output:
输出:
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
)
)
#1
1
Among others, this will work:
除其他外,这将起作用:
^(\w+)\W+\w+\W+(\w+)
Take group \2
and \1
, see a demo on regex101.com (and mind the MULTILINE
modifier).
拿组\2和\1,看看regex101.com上的演示(注意多行修改器)。
As
PHP
code (
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";
}
?>
#2
1
You can use this regex with 2 captured groups in preg_match_all
function:
您可以在preg_match_all函数中使用这个regex和两个捕获的组:
/^(\S+)\h+\S+\h+(\S+)/m
You can then take capture group-2 and capture group-1 to format your output.
然后可以获取capture group-2和capture group-1来格式化输出。
RegEx演示
#3
1
Here is a way to do the job:
下面是一种方法:
$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);
Output:
输出:
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
)
)