Multiple Regular Expressions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Multiple Regular Expressions

Post by jamiel »

Hi there,

I have a different XML Document's which each require different regular expression's performed on them. These regular expressions are going to change over time. At the moment I have a base Parser class which does things which are common to all documents, then an additional class for each document to do the regular expression's which are unique to it. Regular Expressions are not the only manipulation we are doing on these documents.

At the moment my base class has a doRegexReplace() method and in my child classes for the documents I just have a couple $this->doRegexReplace() function calls. However I have some really long nasty replaces in here and because the matches and replace strings are in the code I have no way of writing Unit Tests to test these expressions. The source XML is changing all the time, so I would need to test that just a certain section changes to the new value. Anyone have any ideas where I can put my find -> replace terms to have easy access to them in my code and unit tests? I don't want to use an XML file because the matches and replace's contain XML aswell.

Thanks Jamie
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Is using DOM out of the question? This sounds like exactly the job for something like this.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Use the extract class refactor to remove the behaviour relevent to a find replace out into a separate class and unit test that class.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Hey guys,

Thanks for the responses.

I will be using DOM to do alot of the actual parsing yes. My real question which I don't think I conveyed very well is that I am not sure where to put my actual regular expressions so that my Unit tests can use them. I don't really like the idea of having them directly in the code because some are really long. eg. This is how I am testing them at the moment

Code: Select all

// Example method to tet
public function alterHeader()
{
   $find = '/<div id="header">.*</div>/';
   $replace = '/<div id="alternate_header">My alternate header</div>';
   $this->body = preg_replace($find, $replace, $this->body);
}

//Unit test
public function testAlterHeader()
{
$this->object->alterHeader();
$this->assertTrue(strstr('alterate_header'), $this->object->getBody());
}
Just example code so imagine some big changes, nasty huh? I need some guidance :)

Thanks

Jamie
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

The assertion here is very unclear and you have the wrong number of parameters for strstr

Code: Select all

$this->assertTrue(strstr('alterate_header'), $this->object->getBody());
You should also call setBody() in the test method and use a random value. Other than that I think it's all right.
Post Reply