-1

I want a case insensitive expression that will logically find: (stringA OR stringB) AND stringC.

So if stringA is "dr" stringB is "doctor" and stringC is "presume", I want these results:

Dr. Livinsgston I presume           TRUE
Doctor Livingston I presume         TRUE
Mr. Livingston I presume            FALSE

It does not matter where in the tested string the values reside, though if I could have the expression require that (A or B) precede the C in the tested string, that would be even better.

Is this doable with a regexp?

AJMansfield
  • 4,039
  • 3
  • 29
  • 50
Steve
  • 19
  • 1
  • 3
    in what flavour.. javascript? java? php? e.g. – d'alar'cop Aug 17 '13 at 21:18
  • 1
    Does stringC have to come after stringA/stringB, or can they appear in any order? I would also say that regex is not the only solution to this -- with your fairly simple criteria, you could achieve the same results with a few simple `in_string()` calls (or whatever the relevant function is in whatever language you're working in) – Spudley Aug 17 '13 at 21:20
  • yes or a split followed by some containss – d'alar'cop Aug 17 '13 at 21:21
  • Have you attempted a regular expression yet? – hwnd Aug 17 '13 at 21:36
  • 2
    **Try writing something yourself** and then if it doesn't work, bring it to us to help you along. You start it, we help. We don't write it for you. Show us the actual code that you've tried and then we can help you from there. Chances are you'll get pretty close to the answer if you just try it yourself first. – Andy Lester Aug 17 '13 at 22:47

4 Answers4

1

The Python solution posted above does the job; but on the off-chance that you also just wanted to learn how to do something like this, here's a possible solution (in JavaScript; syntax may vary in other languages):

/(dr|doctor).*?presume/i.test(...);

The i at the end makes it case-insensitive (equivalent to just converting the tested string to lower case beforehand). the | between the words in parenthesis makes it so that these two words can be interchangeable. The .*? just means that there can be pretty much anything between the stuff in parenthesis and presume.

Note that this means that presume has to be before the stuff in parenthesis. Honestly though, if order matters you're in for a lot of pain with regexes.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
psg-1
  • 211
  • 1
  • 3
1

In Perl, you could do something like..

(?:[Dd]r|Doctor).*(?:presume)

Regular expression:

(?:                        group, but do not capture:
  [Dd]                     any character of: 'D', 'd'
     r                     match 'r'
     |                     OR
     Doctor                match 'Doctor'
)                          end of grouping
 .*                        any character except \n (0 or more times)
  (?:                      group, but do not capture (1 or more times)
    presume                match 'presume'
  )                        end of grouping

Short explanation of assertions. See Regex lookahead, lookbehind and atomic groups

(?=)    Positive look ahead assertion
(?!)    Negative look ahead assertion
(?<=)   Positive look behind assertion
(?<!)   Negative look behind assertion
(?>)    Once-only subpatterns 
(?(x))  Conditional subpatterns
(?#)    Comment (?# Pattern does x y or z)
Community
  • 1
  • 1
hwnd
  • 69,796
  • 4
  • 95
  • 132
0

Very doable with regex, but there is absolutely no reason to use regex in this case. You didn't add a language, so here's a simple solution in python:

def check_string(test_string):
    lowered_string = test_string.lower()
    doctor = lambda s: "dr" in s or "doctor" in s
    presume = lambda s: "presume" in s
    return doctor(lowered_string) and presume(lowered_string)

In general you want to avoid using regex whenever possible, and you can easily make a check case-insensitive by just doing your check against a lower-case version of your string (like I did above.)

If you want to match it with a regex here's a version of d'alar'cop's answer that actually works (moved to python to keep my answer internally consistent):

import re
return bool(re.match( r'(dr|doctor).*?presume',test_string.lower()))
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
0

Yes, you can do this with regexp. With grep you can simply do this

echo Doctor Livinsgston I presume | grep "^\(Dr\.\|Doctor\).*presume$" >/dev/null; [[ $? == 0 ]] && echo TRUE || echo FALSE
TRUE
echo Dr. Livinsgston I presume | grep "^\(Dr\.\|Doctor\).*presume$" >/dev/null; [[ $? == 0 ]] && echo TRUE || echo FALSE
TRUE
echo Mr. Livinsgston I presume | grep "^\(Dr\.\|Doctor\).*presume$" >/dev/null; [[ $? == 0 ]] && echo TRUE || echo FALSE
FALSE
bartimar
  • 3,374
  • 3
  • 30
  • 51