Skip to main content
grammar, pcre tag
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

AmI 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 typelike:

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

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

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

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

But thisthat gave me only the second capture group as 0.556984.

alsoAlso 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?

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?

I am using GNU grep with the -P PCRE Regex support for matching strings from a file. The input file has lines containing strings like:

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

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

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

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

But that gave me only the 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?

edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
Source Link
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?