Re: preg_match

From: Date: Thu, 10 Aug 2006 13:49:15 +0000
Subject: Re: preg_match
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-240463@lists.php.net to get a copy of this message
Jon wrote:
preg_match_all("/Charges \s\s+ $total x (.+) /siU", $single, $from_invoice); The text has "Charges" then a variable amount of spaces a previously picked decimal number" x " (and the data I want)" " is this correct or am I missing something? It seems to work sometimes and not others and I have not quite figured out what makes it fail. The error I get is Warning: preg_match_all(): unknown modifier '#' in "file"
Gracious, did you start a long thread. Several workable solutions are provided. I'd like to offer some general advice, I learned from experience. I never use "/" as the delimiter; there are too many chances for errors. I generally use "%", unless my haystack can possible contain it. It is easier to spot syntax errors, for example. Otherwise, I use a delimiter that cannot be in my haystack and is not a regex reserved character. Rarely is there a delimiter that is both in the haystack and is a reserved character; if so, I just use literal code "\" as required. I never use repeated special characters e.g., "\s\s" use "*", "+" or "{n,m}" as needed. I rarely use "dot all" [e.g., "."] it accepts everything and can make testing very difficult. Invariably, there is a combination I never thought of to test. I use explicit statements for everything that can be in my haystack [e.g., \d, \w, [a-z], [0-9], \s, \S, etc.] I always use the Reg Coach for complicated patterns and build and test the pattern one step at a time. Al.........

« previous php.general (#240463) next »