Pepe S
Pepe S

Reputation: 65

Regex select second and third word in a string

I've got a string where I need to aleays select just the second and third wodr in a string. Currently I can only select the first three words but I need to skip the first word. I currently have:

^(?:[^ ]*\ ){2}([^ ]*)

Any help wouild be aprreciated. Thanks!

Upvotes: 0

Views: 108

Answers (2)

The fourth bird
The fourth bird

Reputation: 163217

You can capture the second and the third "word" as in 1 or more non whitespace characters:

^\S+ (\S+ \S+)

Regex demo

Or if supported with a lookbehind:

(?<=^\S+ )\S+ \S+

Regex demo

Upvotes: 1

Cristiano Schiaffella
Cristiano Schiaffella

Reputation: 609

I think this is the regex you're looking for:

(?<=\s)([^ ]*)

Link to example: https://regex101.com/r/wSdzLe/1

Upvotes: 0

Related Questions