several words in any order (how to)

Tips on writing regular expressions for searching the post list

Moderators: Quade, dexter

several words in any order (how to)

Postby bobkoure » Sat Nov 07, 2009 12:22 pm

If you need to find posts with, say, three words in any order, you can go crazy with all the possible orders, separated with '|' chars or...
You could just use a series of "lookahead" operations.
(?=foo) means "followed by foo" and (?=.*foo) means followed by any number of chars and then foo. You can combine this with the line-start char '^' and get ^(?=.*foo) which means "start of line followed by any number of chars and then foo" - and these lookahead expressions can be stacked, so, to get a post with foo, bar and blat in any order you can use: ^(?=.*foo)(?=.*bar)(?=.*blat)

Note that there's also a negative version of this ^(?!.*blat) would be start of line not followed by any number of chars and the blat.
I'll leave it to you how to search for posts with any three words and not another word... Image link not allowed for unregistered users
bobkoure
 

Postby Quade » Sat Nov 07, 2009 1:28 pm

nice
User avatar
Quade
Eternal n00b
Eternal n00b
 
Posts: 44981
Joined: Sat May 19, 2001 12:41 am
Location: Virginia, US

Registered Newsbin User since: 10/24/97


Return to Regular Expressions

Who is online

Users browsing this forum: No registered users and 1 guest