Jump to content

Archived

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

poirot

Regex Question

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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
In addition to Bane's comment, regular expressions also have modifiers. You can't leave the modifier dangling outside of the string ("pattern"i), nor could you assume the last character within the string was a modifier ("patterni").

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Delimiters are important through all programming; they serve as containers which improve and/or allow more complex syntax.

No, I am far from Friedl, but I do enjoy his book.

Share this post


Link to post
Share on other sites
i'm not very good on PHP but it has a good relativity with C.
which i had an experience before.

delimeter is the mark that makes the string to look as string.
i think my grammar is right. and recognize by the php language as a string.
i hope these are correct. he, he, he

Share this post


Link to post
Share on other sites
[quote]effigy, are you Jeffrey Friedl?[/quote]

if i'm not mistaken you are talking about the author of "Mastering Regular Expression".

Share this post


Link to post
Share on other sites
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]

???

Share this post


Link to post
Share on other sites
You still don't have delimiters--they go around your pattern. All metacharacters that should be matched as a literal need to be escaped. In your example that includes[tt] / [/tt] (if used as a delimiter),[tt] .[/tt], and [tt] ?[/tt].

Share this post


Link to post
Share on other sites
How it should look with delimiters then? ??? Sorry, just started learning php.

Share this post


Link to post
Share on other sites
I thought this topic explained it.... See if [url=http://weblogtoolscollection.com/regex/regex.php]this[/url] link helps. Look for "Perl Style Delimiters."

Share this post


Link to post
Share on other sites

×

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.