2

I wanted to build a spamassassin URL that matches:

"inform that the security key has expired"

and variations, where there can be 1 to 3 words before the word "has" and it still has to match.

I keep trying and testing in online regex tool. I used .\w but can make it work only partially with just one word before word "has". I want from 1 to 3 words.

CSᵠ
  • 9,711
  • 9
  • 37
  • 61

2 Answers2

1

To match 1-3 words preceding the word "has" you can use (\w+ ){1,3}has.

Hopefully this has answered your question. That seems to be all you're asking.

sharktamer
  • 126
  • 8
0

Here's a regex that matches 1 to 3 words between the words "key" and "has", and here's a link to a RegExr that tests it. I admit I'm not familiar with spamassassin but if all you need it a regex to match a line of text then this should work:

key(\b\s((\w+\b\s){1,3})has

Using the braces {1,3} means that the previous token (in this case, the group (\w+\b\s)) must match at least once but no more than three times. The \b token matches a word boundary, and \s matches any whitespace. \w, as you know, matches any alphanumeric character.

If you need to, you can use backreferencing to extract group 1, which now contains the words between "key" and "has" (as well as the whitespace surrounding them). Hope this helps!

c.anna
  • 381
  • 1
  • 9