Jump to content

Regex Question


poirot
 Share

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
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
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").
Link to comment
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
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
Link to comment
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
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].
Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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