Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/139106-quick-question/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/139106-quick-question/#findComment-727561
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/139106-quick-question/#findComment-727577
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.