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. 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\)?$/. 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. \( 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_]+\))?$)/ 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? 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. [email protected] and ([email protected]). Thanks, Tim 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. [email protected] and ([email protected])."; preg_match_all('/\b[^@\s]+@\S+?\.[a-z]+\b/i', $data, $matches); print_r($matches) ?> </pre> 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? 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. 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. 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. Link to comment https://forums.phpfreaks.com/topic/121296-solved-allow-parenthesis/#findComment-626066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.