Author Topic: preg_match() [function.preg-match]: No ending delimiter '^' found  (Read 4632 times)

0 Members and 1 Guest are viewing this topic.

Offline bikeriaTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Hello,
i'm a php newbie and i didn't understood how to use the preg_match correctly.
I tried to initiate a ecommerce tool and got a PHP 5.3 environment. I must migrate the ereg-family command to the preg_match statement. So I changed the code to:
                          if  (preg_match($mail_pat, $email, $components)) {
and i got the error: preg_match() [function.preg-match]: No ending delimiter '^' found in

so what must i do?

Thanks a lot.

Offline Alex

  • Global Moderator
  • Addict
  • *
  • Posts: 2,494
  • Gender: Male
  • < 1 billion
    • View Profile
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #1 on: November 25, 2009, 05:14:02 PM »
In PCRE functions you need to use delimiters in your regular expressions. Show us your pattern and we can help you correct it.
:anim_rules: Read the rules, :rtfm: and don't forget to use [code] / [php] tags!


Offline bikeriaTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #2 on: November 25, 2009, 05:27:16 PM »
I didn't understand what do you mean with pattern. This is my code:


 function tep_validate_email($email) {
    $valid_address = true;

    $mail_pat = '^(.+)@(.+)$';
    $valid_chars = "[^] \(\)<>@,;:\.\\\"\[]";
    $atom = "$valid_chars+";
    $quoted_user='(\"[^\"]*\")';
    $word = "($atom|$quoted_user)";
    $user_pat = "^$word(\.$word)*$";
    $ip_domain_pat='^\[([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\]$';
    $domain_pat = "^$atom(\.$atom)*$";

    //if (eregi($mail_pat, $email, $components)) {
    if (preg_match($mail_pat, $email, $components)) {
   
      $user = $components[1];
      $domain = $components[2];
      // validate user
      //if (eregi($user_pat, $user)) {
      if (preg_match($user_pat, $user)) {
     

Offline Alex

  • Global Moderator
  • Addict
  • *
  • Posts: 2,494
  • Gender: Male
  • < 1 billion
    • View Profile
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #3 on: November 25, 2009, 05:34:05 PM »
You need to add delimiters to all your patterns, your patterns are $mail_pat, $domain_pat, etc.. There are a lot of things you can use as delimiters, for the most part it's just down to use preference. Example:

$mail_pat '^(.+)@(.+)$';

Should be:

$mail_pat '~^(.+)@(.+)$~';

Notice the ~ at the start and the end of the pattern.
:anim_rules: Read the rules, :rtfm: and don't forget to use [code] / [php] tags!


Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #4 on: November 27, 2009, 10:32:19 AM »
[ot]
@bikeria

I noticed you made use of .+ Using .+ (or .*) is generally frowned upon, as these quantifiers are greedy by default and will consume as much as they can, then involve some backtracking to achieve the desired end result (I say generally, as it is circumstantial and is sometimes desirable to have it greedy). In cases like this, I would recommend making those quantifiers lazy instead (.+?) - Adding the ? after a quantifier like * or + changes the greediness behavior to lazy. As a result, the regex engine doesn't have to do extra backtracking work.

Damn, since the new forum doesn't have it's bookmark mod installed, I'll have to dig around for a specific thread that discusses this issue more indepth.. if I can find it, I'll post the link in this thread).


Note to self: Bookmark specific threads outside of forum bookmarks in the event upgrades don't have bookmark functionality installed.
[/ot]
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #5 on: November 27, 2009, 10:36:09 AM »
Found it  :)  Post#11 and #14 of that thread will help illustrate laziness vs greediness.
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline salathe

  • Lazy
  • Administrator
  • Addict
  • *
  • Posts: 1,627
  • Gender: Male
  • Temperament: Snuggly
    • View Profile
    • My Blog
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #6 on: November 27, 2009, 12:27:32 PM »
[ot]
Using .+ (or .*) is generally frowned upon…
I wouldn't go that far, to frown upon something simply because it does its job doesn't seem fair at all. Perhaps we should also be frowning on lazy quantifiers in equal measure? For the particular use in this thread (specifically, matching an email address with dot-at-dot style) then sure it might well be worthwhile to be lazy.

As a result, the regex engine doesn't have to do extra backtracking work.
Should be, "In some cases the regex engine doesn't..." There are equally times when using a lazy quantifier would be more work for the engine than otherwise.

P.S. Ditto about the bookmarks, I might start using a phpfreaks tag on delicious for this purpose :shy:
[/ot]
PHP Documentation — Read it, or the badger will come for you.
http://php.net/manual/

Offline nrg_alpha

  • Staff Alumni
  • Addict
  • *
  • Posts: 2,480
  • Gender: Male
  • PHPenchant, I haz it!
    • View Profile
    • Portfolio Site
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #7 on: November 27, 2009, 12:59:21 PM »
Using .+ (or .*) is generally frowned upon…
I wouldn't go that far, to frown upon something simply because it does its job doesn't seem fair at all.

It's not an issue of doing it's job. The issue is a matter of performance (at least in this case anyway). Why make the regex engine work harder than it needs to? If you can avoid backtracking (assuming there might be a fair amount of backtracking involved), then why not do so? Sure, the end result *might* be the same either way (assuming the pattern is written to correctly match the developer's expectations of course). We know that there are more (and less) efficient ways of performing the same task.. as a result, IMO it is fair to say that in general, using greedy quantifiers is frowned upon (many times, performance issues may even take a back seat to accuracy issues, due to backtracking till whatever last instance being matched/captured might not desired).

Quote from: salathe
Perhaps we should also be frowning on lazy quantifiers in equal measure? For the particular use in this thread (specifically, matching an email address with dot-at-dot style) then sure it might well be worthwhile to be lazy.

No, I'm not suggesting lazy quantifiers should also be frowned upon (although in some cases, this may actually be the case, as I mentioned in the previous post, due to circumstances, sometimes greedy quantifiers may indeed be preferable). Given the circumstances I have seen, many people carelessly throw around .* or .+ without understanding it's implications (be it performance or accuracy issues). I do agree that in some cases, it would be advantageous to use greedy quantifiers instead (read: Lazy quantifiers will stifle speed more so than greedy). That's why I mentioned it is circumstancial. Just seems that in general, circumstances seem to favor laziness over greediness more often than not. But like every other tool in the toolbox, there is a situation / circumstance applicable to them.
"Build a man a fire, you warm him for a day. Set a man on fire, and he'll be warm for the rest of his life."
PHP = One cool palindrome!

Offline Daniel0

  • Administrator
  • 'Insane!'
  • *
  • Posts: 11,817
  • Gender: Male
  • ^bb|[^b]{2}$
    • View Profile
Re: preg_match() [function.preg-match]: No ending delimiter '^' found
« Reply #8 on: November 28, 2009, 05:36:54 PM »
[ot]Sorry guys. I'll have to see if I can get that bookmark mod ported to RC2.[/ot]