Kleber S.
Kleber S.

Reputation: 8240

Regex: Extract two specific words

I have a string like this:

1:41 Kill: 1022 2 19: <world> killed Dono da Bola by MOD_FALLING

I need to extract all the characters before killed and after the 3rd. : that is in this case <world> and all characters between by and killed that is in this case Dono da Bola.

Any suggestion?

Thank you in advance.

Upvotes: 0

Views: 46

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

Your regex would be,

^[^:]*:[^:]*:[^:]*:(.*?)killed(.*?)by.*$

DEMO

Group1 contains <world>(ie, space<world>space), and the group2 contains Dono da Bola(ie; space**Dono da Bola**space)

Upvotes: 2

elixenide
elixenide

Reputation: 44823

This will do it:

:[^:]+?:[^:]+?:\s*(.+?) killed (.+?) by .+

Demo.

This matches <world> and Dono da Bola in your example.

Explanation:

  • : matches :
  • [^:]+? matches non-colon characters
  • : matches :
  • [^:]+? matches non-colon characters
  • : matches :
  • \s* matches whitespace
  • (.+?) matches all characters in non-greedy fashion (<world>)
  • killed matches killed and surrounding spaces
  • (.+?) matches all characters in non-greedy fashion (Dono da Bola)
  • by .+ matches space, by, space, and the rest of the string.

Upvotes: 3

Related Questions