1

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156

1 Answers1

2

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.

Aleksei Zyrianov
  • 2,294
  • 1
  • 24
  • 32
  • Alexey it works perfectly, thank you a million! If the last word in the line begins with an asterisk it should match that word only.(though I'm not sure if there are such words in my text). – TotoKalvera Apr 21 '13 at 19:54
  • @TotoKalvera You're welcome. I've added a non-capturing group `(?:...)` to have this case covered too. – Aleksei Zyrianov Apr 21 '13 at 20:03
  • This will only work if there is an alphanumeric character right *before* the asterisk. – Tim Pietzcker Apr 21 '13 at 20:17
  • @TimPietzcker You're right. Thank you. I've fixed that too :) – Aleksei Zyrianov Apr 21 '13 at 20:22
  • 2
    And now...you don't need the `\b` any more because there will always (by definition) be a word boundary between `*` and the following `\w+`. You could do a `(?<!\S)` before the `\*` to make sure that there is either whitespace or the start of the string before the `*` if that is what you're trying to express. – Tim Pietzcker Apr 21 '13 at 20:22
  • I'll try that thank you a lot guys! As a newbie to this, I'd need you to point me to how to export only lines with matches in Regex Buddy or Expresso, I just can't figure out how to do that in either program. I know how to extract lines without results and also how to export only matching results, but I do not know how I can export the whole lines containing matches, and only them. I have all the expression I need and they work perfectly, yours from earlier today Tim and Aleksey's now, now I only need to put it to work. – TotoKalvera Apr 21 '13 at 20:29
  • @TimPietzcker Thank you again. The new version has your fix included :) – Aleksei Zyrianov Apr 21 '13 at 20:34
  • @TotoKalvera In Expresso you may `Run Match`, then the result will appear in `Search Results`. It can be copied by right-clicking on the `Search Results` area and choosing `Copy Matched Text to Clipboard`. The result would be separated by a newline. – Aleksei Zyrianov Apr 21 '13 at 20:45
  • Thank you Alexey! I know I can do that, but I need the complete lines with the matching expression in them to be copied, not only the search results. I have to separate lines that contain the match from those that do not. In Expresso, when I validate the results I get a tick beside the lines that contain the match and X beside those that don't, now all I need it is to export the matching lines to txt file, which I still can't figure out how to do. – TotoKalvera Apr 21 '13 at 20:55
  • @TotoKalvera I've supplemented my answer with an approach you may use to get the matching lines. – Aleksei Zyrianov Apr 21 '13 at 21:12
  • The expression doesn't return any matches. I've ticked the multiline box in Options in Design Mode and tried but no matching results with the latter expression :( – TotoKalvera Apr 21 '13 at 21:27
  • @TotoKalvera Not the one that's in `Options`, but the one that's among checkboxes at the windows bottom. – Aleksei Zyrianov Apr 21 '13 at 21:32