How would I detect if a string contained for instance my first name but not my last name using only a regular expression?
-
This is actually quite simple. Read up on this simple REGEX tutorial and it covers this quite nicely. http://www.codeproject.com/KB/dotnet/regextutorial.aspx – CamelSlack Oct 20 '11 at 17:35
-
5You don't need regex for this. Just do `str.contains(first_name) && !str.contains(last_name)` – NullUserException Oct 20 '11 at 17:36
-
4You should always specify the language you are using regexes from. It's even written in the tag. Every language implements regexes in a different way. – xanatos Oct 20 '11 at 17:51
3 Answers
The easiest way to use regular expressions for this is to use simple regexes and logical connectives. Since you only want simple matches and since you didn't list a language, here is a basic implementation in Perl:
my $str1="firstname lastname blah blah blah";
my $str2="blurg firstname etc";
foreach($str1,$str2)
{
if(/firstname/ and !/lastname/)
{
print "$_ matched firstname and not lastname!\n";
}
else
{
print "No match for $_\n";
}
}
As expected, the output is:
No match for firstname lastname blah blah blah
blurg firstname etc matched firstname and not lastname!
^(?=.*firstname)(?=.*lastname)
With many regex versions you can make something like this. I'm using zero-width lookahead to search for your firstname and your lastname. They don't "move" the regex cursor, so both are scanned starting from the first character. The regex will fail if firstname or lastname isn't present.
Seriously, the regex should be more complex, otherwise you could have these situations:
firstname = `name`
lastname = `lastname`
lastname // ok with the given rules
and with
firstname = `firstname`
lastname = `lastname`
firstnamelastname // ok with the given rules, even without the space
xfirstnamex xlastnamex // ok with the given rules
The regex would need to be:
^(.*\bfirstname\b.*\blastname\b)|(.*\blastname\b.*\bfirstname\b)
so checking for both orders of firstname and lastname and checking that there is a word separator before and after firstname and lastname.
I'll add that what I have showed are perfect examples of thing not to do. You want to user regexes? You don't! First you try to use the string functions of your language. Then, if they fail, you can try with regexes.

- 109,618
- 12
- 197
- 280
-
-
@JackManey You can think what you want, but it would handle it. http://gskinner.com/RegExr/?2uvv1 It has other problems. – xanatos Oct 20 '11 at 17:53
-
@JackManey The problems are `firstnamelastname` would be considered correct, or if your firstname is `name` and your lastname is `lastname` then `lastname` would be ok (`lastname` contains both `name` and `lastname`) – xanatos Oct 20 '11 at 17:54
How about:
#!/usr/bin/perl
use Modern::Perl;
while (<DATA>) {
chomp;
say /^(?=.*\bfirstname\b)(?!.*\blastname\b)/ ? "OK : $_" : "KO : $_";
}
__DATA__
jhjg firstname jkhkjh lastname kljh
jhgj lastname kjhjk firstname kjhkjh
jhgdf firstname sjhdfg not_my_lastname
jshgdf no_my_lastname jhg firstname jkhghg
output:
KO : jhjg firstname jkhkjh lastname kljh
KO : jhgj lastname kjhjk firstname kjhkjh
OK : jhgdf firstname sjhdfg not_my_lastname
OK : jshgdf no_my_lastname jhg firstname jkhghg

- 89,455
- 62
- 89
- 125