bikeria Posted November 25, 2009 Share Posted November 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/ Share on other sites More sharing options...
Alex Posted November 25, 2009 Share Posted November 25, 2009 In PCRE functions you need to use delimiters in your regular expressions. Show us your pattern and we can help you correct it. Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-965697 Share on other sites More sharing options...
bikeria Posted November 25, 2009 Author Share Posted November 25, 2009 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)) { Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-965701 Share on other sites More sharing options...
Alex Posted November 25, 2009 Share Posted November 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-965704 Share on other sites More sharing options...
nrg_alpha Posted November 27, 2009 Share Posted November 27, 2009 [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] Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-966458 Share on other sites More sharing options...
nrg_alpha Posted November 27, 2009 Share Posted November 27, 2009 Found it Post#11 and #14 of that thread will help illustrate laziness vs greediness. Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-966459 Share on other sites More sharing options...
salathe Posted November 27, 2009 Share Posted November 27, 2009 [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 [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-966488 Share on other sites More sharing options...
nrg_alpha Posted November 27, 2009 Share Posted November 27, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-966501 Share on other sites More sharing options...
Daniel0 Posted November 28, 2009 Share Posted November 28, 2009 [ot]Sorry guys. I'll have to see if I can get that bookmark mod ported to RC2.[/ot] Quote Link to comment https://forums.phpfreaks.com/topic/182957-preg_match-functionpreg-match-no-ending-delimiter-found/#findComment-967170 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.