Jump to content


Photo

Regex Question


  • Please log in to reply
10 replies to this topic

#1 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 13 June 2006 - 06:31 PM

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
~ D Kuang

#2 Koobi

Koobi
  • Staff Alumni
  • Advanced Member
  • 419 posts
  • LocationColombo, Sri Lanka | South Asia

Posted 13 June 2006 - 07:26 PM

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
<?php
    //example: match either 4 or more lower case alphabets
    preg_match('/[a-z]{4,}/', ...);
?>


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




i used my own delimiters instead of the common one (/)
<?php
    //example: match a simple url
    preg_match('%^http://[a-z]+\.com/%', ...);
?>


#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 June 2006 - 07:56 PM

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").
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 01:01 AM

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?
~ D Kuang

#5 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 June 2006 - 02:39 PM

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.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#6 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 14 June 2006 - 04:08 PM

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

#7 bilis_money

bilis_money
  • Members
  • PipPipPip
  • Advanced Member
  • 621 posts

Posted 14 June 2006 - 04:31 PM

effigy, are you Jeffrey Friedl?


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



#8 DjiXas

DjiXas
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2007 - 02:25 PM

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

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

Make it like this:

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

???

#9 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2007 - 02:41 PM

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 / (if used as a delimiter), ., and ?.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#10 DjiXas

DjiXas
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 02 October 2007 - 09:15 PM

How it should look with delimiters then? ??? Sorry, just started learning php.

#11 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 October 2007 - 09:27 PM

I thought this topic explained it.... See if this link helps. Look for "Perl Style Delimiters."
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users