user1547410
user1547410

Reputation: 893

regex, get the first characters after the line when you find a keyword

Ive been mashing with regex builders and tutorials and cant figure this out. I have a keyword which i can find and i need to find the timestamp on either that same line or the next line.

my text looks something like this

16:51:35.536 This is a bunch of completely random text although it has a keyword. This keyword is unique
16:51:35.806 This is another line which has all sorts of crap i dont really care about.

Now what im trying to capture is either the first 8 characters to get the timestamp ie 16:51:35

so im trying to do something like this

it has a keyword (.*) ([0-9){2}:[0-9){2}:[0-9){2}) 

but for whatever reason it gets stuck on that line even if i add /m at the end or /s or /S

This works on getting to the next line:

([\s\S]*)

so i tried

it has a keyword ([\s\S]*)(?=[0-9){2}:[0-9){2}:[0-9){2}) but that didnt work either

Im pretty sure its less complicated than im making it, any ideas

Upvotes: 0

Views: 78

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29399

If you want the first eight characters of a line with the keyword string 'foobar', then you can use:

^(.{8})[^\n]*foobar.*?$

You can see this work at http://rubular.com/r/gW8GCRrYkg This is easily modified to get the first eight characters of the next line instead or to put constraints on the first eight characters.

Upvotes: 1

Related Questions