Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep: group capturing

I have following string:

{"_id":"scheme_version","_rev":"4-cad1842a7646b4497066e09c3788e724","scheme_version":1234} 

and I need to get value of "scheme version", which is 1234 in this example.

I have tried

grep -Eo "\"scheme_version\":(\w*)" 

however it returns

"scheme_version":1234 

How can I make it? I know I can add sed call, but I would prefer to do it with single grep.

like image 354
lstipakov Avatar asked Dec 22 '11 10:12

lstipakov


People also ask

What is a capturing group regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is non-capturing group in regex?

Non-capturing groups are important constructs within Java Regular Expressions. They create a sub-pattern that functions as a single unit but does not save the matched character sequence. In this tutorial, we'll explore how to use non-capturing groups in Java Regular Expressions.

Can I use named capturing groups?

Mixing named and numbered capturing groups is not recommended because flavors are inconsistent in how the groups are numbered. If a group doesn't need to have a name, make it non-capturing using the (?:group) syntax. In . NET you can make all unnamed groups non-capturing by setting RegexOptions.

What is capturing group in regex Javascript?

A part of a pattern can be enclosed in parentheses (...) . This is called a “capturing group”. That has two effects: It allows to get a part of the match as a separate item in the result array.


1 Answers

You'll need to use a look behind assertion so that it isn't included in the match:

grep -Po '(?<=scheme_version":)[0-9]+'

like image 139
SiegeX Avatar answered Sep 19 '22 15:09

SiegeX