I want to match either @ or 'at' in a regex. Can someone help? I tried using the ? operator, giving me /@?(at)?/ but that didn't work
+12
A:
Try:
/(@|at)/
This means either @
or at
but not both. It's also captured in a group, so you can later access the exact match through a backreference if you want to.
Michael Myers
2009-07-27 14:34:52
would this handle cases where the email address is something like [email protected], or latitudeataddress.com?
Josh E
2009-07-27 14:50:28
Why wouldn't it? What do you mean by "handle"?
Michael Myers
2009-07-27 14:56:59
would it produce false positive matches is what I'm asking I guess.
Josh E
2009-07-27 15:17:36
Of course it would produce false positives! Regular expressions can't read your mind.
innaM
2009-07-27 15:18:51
right... so then would the OP be better served with a regex that attempts to reduce that possiblity in your opinion? I think that you could improve upon it, but at the cost of increased complexity. Would that be worth it though?
Josh E
2009-07-27 15:21:47
+8
A:
/(?:@|at)/
mmyers' answer will perform a paren capture; mine won't. Which you should use depends on whether you want the paren capture.
chaos
2009-07-27 14:35:11
Aw, thanks. Have a +1 toward the Enlightened badge you'll be getting for this one. :)
chaos
2009-07-27 14:45:16
A:
have you tried
@|at
that works for (in the .NET regex flavor) the following text
[email protected] johnsmithatgmail.com
Josh E
2009-07-27 14:35:59
+3
A:
if that's only 2 things you want to capture, no need regex
if ( strpos($string,"@")!==FALSE || strpos($string,"at") !==FALSE ) {
# do your thing
}
ghostdog74
2009-07-27 14:46:55
True. I was assuming this was part of something bigger, but if not, this is probably better.
Michael Myers
2009-07-27 14:50:43