slpctrl Posted January 1, 2009 Share Posted January 1, 2009 I've noticed a lot of regex syntaxes start off differently, for example I could see the following: preg_match_all("/[\w]/", $string, $matches); Where, immediately after the quotes the '/' is used. I've also seen: preg_match_all('#<a\b[^>]*href="http://(.*)nmousedown="return clk\(this\.href,\'\',\'\',\'res\',\'(?:10|[0-9]),\'\'\)"[^>]*>\z#U', $result, $matches); Where the '#' is used, and I also see: preg_match_all("|~<a href=\"([^\"]+)\"~|U", $result, $matches); Where the '|' is used. What is the difference and does it actually change the pattern? Quote Link to comment https://forums.phpfreaks.com/topic/139106-quick-question/ Share on other sites More sharing options...
nrg_alpha Posted January 1, 2009 Share Posted January 1, 2009 Those are delimiters (which is required in preg functions).. You can use (almost anything) as delimiters (you can use any non-alphanumeric, non-whitespace ASCII characters.. but not backslashes). As you are noticing, you can use /..../, #.....# or ~....~ for starters... some people use !.....! You can even use opening delimiters like { < [ (, but in these cases, you must use the opposites as closing } > ] ).I personally do not recommend using those, as they are very common characters used within patterns, and you'll need to escape them. I tend to gravitate towards #...# myself as a matter of personal preference.. but use what is most comfortable for you. EDIT.. another reason to avoid especially <...> is bacause this could fool people into thinking this is part of the pattern that is using these as part of tags, when meanwhile they are simply the opening / closing delimiter characters.. so one really must be careful when using such things. Quote Link to comment https://forums.phpfreaks.com/topic/139106-quick-question/#findComment-727561 Share on other sites More sharing options...
.josh Posted January 1, 2009 Share Posted January 1, 2009 I used to use / .. / but since that symbol comes up a lot in regexing html (which is a common thing for people to use regex for, for web dev), I migrated to ~ .. ~ Quote Link to comment https://forums.phpfreaks.com/topic/139106-quick-question/#findComment-727564 Share on other sites More sharing options...
.josh Posted January 1, 2009 Share Posted January 1, 2009 btw to add on to what nrg_alpha said... by 'delimiter' he means that they signify the start and end of your pattern. The reason why you need them is because with php (and a lot of other languages), modifiers are parsed from withing the string, so php has to know where the actual pattern starts and ends, and it looks at what is outside of the delimiters for the modifiers (like 'i' for case insensitive matching, etc..) for example: ~[a-zA-Z]~ would match a lowercase or uppercase letter. But you can do the same thing with this: ~[a-z]~i but note that the i modifier would not just apply to that class; it would apply to everything else in your pattern, as well. Quote Link to comment https://forums.phpfreaks.com/topic/139106-quick-question/#findComment-727577 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.