Search code examples
regexmatchregexbuddy

Match words beginning with an asterisk and the following word using regex look ahead function


I need a regex expression which will match words that begin with an asterisk and the word that follows them. It will match eismoud tempor and dolore magna in the example below:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do *eiusmod tempor incididunt ut labore et *dolore magna aliqua.

I'm using RegexBuddy.


Solution

  • Try this expression, please:

    (?<!\S)\*\w+(?:[^\w]+\w+)?\b
    

    To catch all the lines matching the regex you might replace the expression with the a line-capturing one:

    ^.*(?<!\S)\*\w+(?:[^\w]+\w+)?\b.*$
    

    and then setting in Design mode of Espresso the Multiline option (the one at the window bottom, not in Options tab). Et voilà! The search results now contain all the lines that include our matches.