Jump to content

preg_match() [function.preg-match]: No ending delimiter '^' found


Recommended Posts

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.

 

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)) {

     

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.

[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]

[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]

 

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).

 

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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