Page 1 of 1

A really frustrating regexp won't match!

Posted: Wed Jun 07, 2006 10:44 am
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.

Posted: Wed Jun 07, 2006 11:31 am
by feyd
probably has something to do with this:

Code: Select all

[$@myels.com]

Posted: Wed Jun 07, 2006 11:54 am
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?

Posted: Wed Jun 07, 2006 12:09 pm
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,'

Posted: Wed Jun 07, 2006 12:19 pm
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]);
}

Posted: Thu Jun 08, 2006 6:34 am
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.

Posted: Thu Jun 08, 2006 6:46 am
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]);
}