Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use regex to grab the eighth word

New to Regex Examples I've seen show searching for very specific exceptions ie. specific letter combos.

What I want is to grab the 8th word no matter what comes before,no matter what those words are.

So the spaces are what designates 'words'

Sample line would be

Sep 20 11:13:18 10.50.3.100 Sep 20 11:13:15 DC1ASM1.dcl.greendotcorp.com Blah Blah Blah

I want to extract the host name, in this case "DC1ASM1.dcl.greendotcorp.com", which is always preceded by "Month, Day, Timestamp, IP, Month, Day, Timestamp" pattern.

Thanks Rex

like image 846
user1917424 Avatar asked Dec 20 '12 01:12

user1917424


1 Answers

I'm not 100% sure what version or flavor of regex you're using, so I'll avoid the look-behind and use a non-capturing group instead:

^(?:\S+?\s){7}(\S+)

That binds to the beginning of the line, ignores 7 consecutive patterns of [any character but whitespace] 1+ times] then [one single whitespsace character].

You can be more specific about "words" by using \w instead of \S if you so chose, though.

like image 176
Brad Christie Avatar answered Sep 25 '22 19:09

Brad Christie