On 2016-04-08 14:02, Robert Boyl wrote:
> 
> describe TEST123    test
> body     TEST123        /\bNeed to buy products *\?\b/i
> score    TEST123 0.0
> 
> If possible, also make it catch if more than 1 question mark :)
> use \ in front of space char
> 
Try this:

describe TEST123    test
body     TEST123        /Need to buy products\s*\?+/i
score    TEST123 0.0

\s*  matches zero or more whitespace (spaces and TABs)
\?+ matches one or more question marks

You can use 'grep -P' or any of the online regex testers to try out a
regex before writing it into a rule. The -P option tells grep to use
Perl regular expressions, which may differ from standard grep ones. I
checked the regex I showed above this way.

Input was this set of seven lines in a file called need.txt:

Need to buy products? This and the next four lines should match.
Need to buy products ?
A need to buy products  ?
Need to buy products     ??
Do you need to buy products ? We have some.
Need to buy products - doesn't match
Want to but products - doesn't match

The test command, where the options -P tells grep to use a Perl regex
and -i means caseless matching, was:

grep -Pi 'Need to buy products\s*\?+' need.txt

where the regex is exactly equivalent to:

/Need to buy products\s*\?+/i

and the output was five matched lines:

Need to buy products? This and the next four lines should match.
Need to buy products ?
A need to buy products  ?
Need to buy products     ??
Do you need to buy products ? We have some. 

When in doubt about writing a regex, this is my usual way of rapidly
finding a valid expression the checking that it matches what I expect.
On my system grep defaults to highlighting the matched text in red.

My standard reference for writing Perl regexes is the O'Reilly 'Camel
book', "Programming Perl" chapter 5. 

 
Martin

Reply via email to