2

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.

4
  • Is there a reason why this would be a string and not an array? Commented Mar 16, 2017 at 11:42
  • An array is ok too. Commented Mar 16, 2017 at 11:42
  • If you have the choice to save this as an array or string, I would recommend saving this as an array. If this comes from an external source and you cant have it as an array, then ignore me. Commented Mar 16, 2017 at 11:46
  • s first_index - green a typo? Commented Mar 16, 2017 at 11:46

3 Answers 3

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).


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";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.

RegEx Demo

Comments

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.