A really frustrating regexp won't match!

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

A really frustrating regexp won't match!

Post by hydroxide »

Code: Select all

/(Processing Location\:\s)([A-Za-z\,\s]*)(\&lt\;)(\D\w\s[a-zA-Z0-9]*\D\D[a-zA-Z0-9\'\:\@]*[$@myels.com]*)(\D\D)([a-zA-Z0-9]*[$@myels.com]*)(<\/a>&gt\;(<br>)[\s]*<)((\D\w) (href)\D\Dmailto\:[a-zA-Z0-9]*[$@myels.com]*)(\D>)([a-zA-Z0-9]*[$@myels.com]*)(<\/a>>)(<br>)/

REFUSES to match with

Code: Select all

Processing Location: bradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br>
It just outputs to the screen 'bradenton,' and that's it. It won't display any more than that. I just can't figure out what's wrong. Any help appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

probably has something to do with this:

Code: Select all

[$@myels.com]
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

seems pretty odd that you are using \D (any non-numeric character) to match < and '>

What are you really trying to extact from the string?
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

The whole thing, actually. I got rid of using the myels.com thing, but it's still not matching any more than 'bradenton,'
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

I think this may be what you are trying to do, expressed as a SimpleTest test case:

Code: Select all

function testMatchPreambleAndEmail() {
$str = "some glick
Processing Location: bradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br> 
more stuff...";
$target = "Processing Location: bradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br>";
$regex = "~(Processing Location:\s+\w+,(?:.*?<<a\s+[^>]+.*?</a>><br>){2})~ims";

preg_match($regex, $str, $match);
$this->assertEqual($target, $match[1]);
}
User avatar
hydroxide
Forum Commoner
Posts: 77
Joined: Mon Jun 05, 2006 9:53 am

Post by hydroxide »

The problem is, that I'm needing to match records in a large file that contain many entries that follow the same format as that one, only with different information. It wouldn't be possible to enter each one (there's other parts to the file besides this one element) in that function.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

hydroxide wrote:The problem is, that I'm needing to match records in a large file that contain many entries that follow the same format as that one, only with different information. It wouldn't be possible to enter each one (there's other parts to the file besides this one element) in that function.
Ok, so change it to preg_match_all()

Code: Select all

function testMatchPreambleAndEmail() {
$str = "some glick
Processing Location: bradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br> 
more stuff...
Processing Location: newbradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br> 
";
$target = array("Processing Location: bradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br>"
 ,"Processing Location: newbradenton, <<a href='mailto:rrivera@myels.com'>rrivera@myels.com</a>><br>
 <<a href='mailto:faffolter@myels.com'>faffolter@myels.com</a>><br>");
$regex = "~(Processing Location:\s+\w+,(?:.*?<<a\s+[^>]+.*?</a>><br>){2})~ims";

preg_match_all($regex, $str, $match);
$this->assertEqual($target, $match[1]);
}
Post Reply