Maq Posted August 25, 2008 Share Posted August 25, 2008 I have a regular expression "/({0,1}^(*[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)$){0,1}/" I want to be able to recognize and email address by itself or have parenthesis around it. I tried adding (* in the beginning and )* at the end but PHP doesn't like it. I'm not very good with regex, and help is appreciated. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/ Share on other sites More sharing options...
effigy Posted August 25, 2008 Share Posted August 25, 2008 Parentheses are metacharacters, so they must be escaped if you want a literal: \( \). Also, use ? instead of {0,1}. You need to keep your anchors in place to have them take effect: /^\(?pattern\)?$/. Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625322 Share on other sites More sharing options...
DarkWater Posted August 25, 2008 Share Posted August 25, 2008 You need to escape them because they're a valid regex character. \( Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625323 Share on other sites More sharing options...
Maq Posted August 25, 2008 Author Share Posted August 25, 2008 Like this: /(^(\(?*[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+\))?$)/ Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625380 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 Why so many parentheses? What preg_ function are you using? Are you using the matches? Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625948 Share on other sites More sharing options...
Maq Posted August 26, 2008 Author Share Posted August 26, 2008 I'm using preg_match_all. Sorry, I'm just not familiar with regex because I usually just google what I need. But overall I just need to find a regex to locate emails with and without parenthesis around it. e.g. tim@yahoo.com and (tim@yahoo.com). Thanks, Tim Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625954 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 How about something generic like this? <pre> <?php $data = "I'm using preg_match_all. Sorry, I'm just not familiar with regex because I usually just google what I need. But overall I just need to find a regex to locate emails with and without parenthesis around it. e.g. tim@yahoo.com and (tim@yahoo.com)."; preg_match_all('/\b[^@\s]+@\S+?\.[a-z]+\b/i', $data, $matches); print_r($matches) ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-625995 Share on other sites More sharing options...
Maq Posted August 26, 2008 Author Share Posted August 26, 2008 Great, thanks effigy, works perfectly. Could I ask what the purpose of the 'i' at the end is for? Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-626002 Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 Case insensitivity. Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-626051 Share on other sites More sharing options...
Maq Posted August 26, 2008 Author Share Posted August 26, 2008 Oh ok thanks. I took a class on foundations and learned about machines and regular expressions but don't remember anything, as you can see. Thanks for your help though, bye. Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-626063 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 Anything after the ending delimiter is considered a modifier. Quote Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-626066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.