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

前端 未结 6 972
借酒劲吻你
借酒劲吻你 2020-12-01 12:19

For example, in this text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eu tellus vel nunc pretium lacinia. Proin sed lorem. Cras sed

6条回答
  •  暖寄归人
    2020-12-01 12:47

    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.)

提交回复
热议问题