AlienDeg
AlienDeg

Reputation: 1339

How to extract first "word" after bracket

Sorry for asking that kind of question, but I can't find a way and I has been working on this last 2 hours. So I was able to create pattern for bracket and name which is \\) \\w\\.\\w+","//1 but I can't find a way to write a pattern just for a name and how to extract this name from the string and assign to variable.

here is example of string

"(7:54) (Shotgun) B.Hoyer sacked at JAC 49 for -7 yards (R.Miller). FUMBLES (R.Miller), recovered by HOU-X.Su'a-Filo at JAC 50. X.Su'a-Filo to JAC 42 for 8 yards (T.Alualu; A.Branch)."

and here is expected result

B.Hoyer

Upvotes: 1

Views: 850

Answers (2)

saml
saml

Reputation: 794

You'll need to re-escape this i.e. re-add double slashes where required, but would something like this work?

^\(\d{1,2}:\d{1,2}\)\s\(.*\)\s(.*?)\s.*$

They key lay in the (.*?)\s which is any number of characters up to the space, lazily i.e. the matching group will terminate at the first space.

EDIT: See Dawg's note below explaining that ^(?:[^)]*\)){2}\s*([\w.]+) is substantially more efficient...

Upvotes: 3

dawg
dawg

Reputation: 104034

You can do:

^(?:[^)]*\)){2}\s*([\w.]+)

Demo

Upvotes: 1

Related Questions