How to match the first word after an expression with regex?

This sounds like a job for lookbehinds, though you should be aware that not all regex flavors support them. In your example:

(?<=\bipsum\s)(\w+)

This will match any sequence of letter characters which follows “ipsum” as a whole word followed by a space. It does not match “ipsum” itself, you don’t need to worry about reinserting it in the case of, e.g. replacements.

As I said, though, some flavors (JavaScript, for example) don’t support lookbehind at all. Many others (most, in fact) only support “fixed width” lookbehinds — so you could use this example but not any of the repetition operators. (In other words, (?<=\b\w+\s+)(\w+) wouldn’t work.)

Leave a Comment