Ben
Ben

Reputation: 1116

Selecting the Word after a particular word using RegEx (Regular Expression)

In short i am trying to match a word that is after a particular word.

So i have a String that is

Name=James  
Age=55  
City=New York  

Now i want to select everything after the "Age=" but not including the "Age=".

So in short i only want to select "55". There are new line char at the end of each line. Now i've looked at the Lookaround like
(?!(Age)).*\r

Which doesn't work.

open to suggestion here.

Upvotes: 2

Views: 2221

Answers (4)

Amey P Naik
Amey P Naik

Reputation: 718

The answer is (?<=Age=)\w+

\w matches any word character (a-z,0-9)

Upvotes: 0

Donald Miner
Donald Miner

Reputation: 39893

What you are looking for is "positive lookbehind"

(?<=your pattern)

This looks behind the current location and it needs to match.

So in your case, you want to do:

(?<=Age=).*$

Upvotes: 1

Kent
Kent

Reputation: 195059

"(?<=Age=).*$"

kent$ echo "Name=James  
dquote> Age=55  
dquote> City=New York "|grep -Po "(?<=Age=).*$"
55

Upvotes: 0

xanatos
xanatos

Reputation: 111860

Try this:

(?<=Age=).*

You have to use a lookbehind.

Be aware that non all the regexes have lookbehind (for example Javascript's one doesn't)

Upvotes: 0

Related Questions