Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
"AND" and "OR" in regular expressions

Posted by enquirer (enquirer), 11 September 2003
I am very new to regular expressions and I have managed to build a simple search using Case sensitivity, folder selection a logical OR statement. But, how do you perform a logicalAND.I am using ASP and I am very stuck.


Posted by admin (Graham Ellis), 11 September 2003
If you want to look for a string that contains both abc and def (for example), write
                 abc.*def
(that's dot star between the abc and def), and that will match if the input string contains abc followed later by def.

If you don't know whether the abc or def will come first (if, for example, you're writing a search algorithm to find pieces of data that contain every one of a number of elements), don't try and write a single complicated regular expression - write a loop instead.  I don't
know what language you're using within ASP - personally I like Perl; here's a Perl example that checks if a text string contains abc and def:

Code:
@want = ("abc","def");
$lookintext = "This is the text that definitely contains abc";
$matched = 1;
foreach $current (@want) {
                   $matched = 0 if ($lookintext !~ /$current/) ;
         }


Works for any number of search strings in any order, and returns you a variable called $matched with a true value (1) if it matches all, false (0) otherwise.




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho