Jump to content

Regex Question


poirot

Recommended Posts

I think I finally grasped Regex... Everything seems to be easier =)

There is still something that bugs me... What are pattern delimiters for?
I know you need them, but why? Is there a specific reason for the need of delimiters?

When you use for example preg_match("pattern", ...) why can't PHP understand "pattern" is a regex pattern and "forget" the delimiter?

Thanks in advance
Link to comment
https://forums.phpfreaks.com/topic/11905-regex-question/
Share on other sites

i think it's because PHP is just the glue language. it needs to pass on this regex to the regex engine which is independant of PHP's engine.

And PHP won't add the delimiters on it's own because what if your pattern consisted of an instance of that delimiter? You would have to escape it so that it is taken literally...and that can be annoying if you have many instances of that delimiter, therefore, PHP gives us the option to specify the delimiters as well.



case in point:

this is fine
[code]
<?php
    //example: match either 4 or more lower case alphabets
    preg_match('/[a-z]{4,}/', ...);
?>
[/code]


this looks messy
[code]
<?php
    //example: match a simple url
    preg_match('/^http:\/\/[a-z]+\.com\//', ...);
?>
[/code]




i used my own delimiters instead of the common one (/)
[code]
<?php
    //example: match a simple url
    preg_match('%^http://[a-z]+\.com/%', ...);
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/11905-regex-question/#findComment-45182
Share on other sites

I guess effigy's comment makes more sense, since you only need to escape delimiters/use different delimiters because of their existence itself. PHP being a glue language is not an answer, because this would only change the question to the regex engine: "why does the regex need delimiters"?

Thanks

effigy, are you Jeffrey Friedl?
Link to comment
https://forums.phpfreaks.com/topic/11905-regex-question/#findComment-45306
Share on other sites

  • 1 year later...
Can anyone tell me what symbols needs to be seperated with \ and how exactly to seperate, just put \ before any symbol?

Like if url is

[code]preg_match_all('http://www.domain.com/index.php?article=(.*)')[/code]

Make it like this:

[code]preg_match_all('http\:\/\/\www\.domain\.com\/index\.php\?article\=(.*)')[/code]

???
Link to comment
https://forums.phpfreaks.com/topic/11905-regex-question/#findComment-360089
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.