Skip to main content
1 of 3
Inian
  • 13.1k
  • 2
  • 42
  • 55

PCRE-regex Use grep to exclude a capturing group

Am using GNU grep with the -P PCRE Regex support for matching a strings from a file. The input file has lines containing the strings of type

FOO_1BAR.zoo.2.someString:More-RandomString (string here too): 0.45654343

I want to capture the numbers 2 and 0.45654343 from the above line. I used a regEx

grep -Po ".zoo.\K[\d+](.*):\ (.*)$" file

But this is producing me a result as

2.someString:More-RandomString (string here too): 0.45654343

Am able to get the first number from first capturing group as 2, also have matched a capturing group at the end of the line. But am not able to skip the words/lines between two capturing groups.

I know for a fact that I have a group (.*) it is capturing those words in the middle. What I tried to do is include another \K to ignore as

grep -Po ".zoo.\K[\d+](.*):\K (.*)$" file

But this gave me only second capture group as 0.556984

also with a non-capturing group with the (?:) syntax as

grep -Po ".zoo.\K[\d+](?=.someString:More-RandomString (string here too)):\ (.*)$"

But this gave me nothing. What am I missing here?

Inian
  • 13.1k
  • 2
  • 42
  • 55