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
?
"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.
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).
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] . "
";
}
?>
You can use this regex with 2 captured groups in preg_match_all
function:
/^(\S+)\h+\S+\h+(\S+)/m
You can then take capture group-2 and capture group-1 to format your output.
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
)
)