3

I have an example string:

Ole Abraham  of XYZ becomes Chief Digital Officer and EVP, Business Development of Warner Music Group.

I want all the words from the last occurrence of of till .. Result required: of Warner Music Group.

I have used the RegEx: (\bof\b).+\.\s? This is returning the sub-string from the first occurance of of:

of XYZ becomes Chief Digital Officer and EVP, Business Development of Warner Music Group. 
halfer
  • 19,824
  • 17
  • 99
  • 186
Sonu Kumar
  • 108
  • 6

1 Answers1

2

Regex engines process the string from left to right, thus, the of your pattern matches is the first from the left. Then, .+ matches the whole line to its end, and only backtracks to find . (the \s? is not important, as it can match an empty string, but will be matched if found right after . since ? is a greedy quantifier).

You may use

.*\bof\s+([^.]+)

See the regex demo. The result will be in Group 1. Note that if you must test for the . presence in the string, append \. to the end of the pattern.

Details

  • .* - will match any 0+ chars other than line break chars, as many as possible, up to the last occurrence of the subsequent subpatterns
  • \bof - a whole word of
  • \s+ - 1 or more whitespaces
  • ([^.]+) - one or more chars other than ..
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I am a novice user to Regex but demo is really helpful. Could you please suggest how can I capture the group match only and not the full match. – Sonu Kumar Jun 30 '17 at 08:19
  • @SonuKumar: What is the regex flavor? What computer language are you using? In almost all cases, you do not need to get the full match, you may access *captures* using appropriate language means. – Wiktor Stribiżew Jun 30 '17 at 08:20
  • am using VB Script – Sonu Kumar Jun 30 '17 at 08:21
  • Then use `match.Submatches(0)` to access that value. See [this answer of mine](https://stackoverflow.com/questions/42273183/vbscript-regex-find-block-of-data-between-a-pattern/42273297#42273297) for a code snippet. – Wiktor Stribiżew Jun 30 '17 at 08:23