[REGEX]Matching strings

Associate
Joined
21 May 2003
Posts
1,365
Right, I'm clueless with regular expressions so I was wondering if someone could give me a hand.

I basically need to match the string

class/helper/<anthing here except mysql>

i.e.
class/helper/transactionhelper.class.php
class/helper/userhelper.class.php
but not
class/helper/mysql/producthelper.class.php
etc

No doubt this is extremely easy for one of you guys. Thanks in advance.
 
Always plenty of ways to run a regexp - the following works:

PHP:
<?php
$s = 'class/helper/transactionhelper.class.php';
//$s = 'class/helper/userhelper.class.php';
//$s = 'class/helper/mysql/producthelper.class.php';

$p = preg_match('|^class/helper/[^/]+$|is', $s, $m);

echo '<pre>'; var_dump($m);

That regexp says that a match must start with (^) 'class/helper/' and after that there must not be a forward slash ([^/]+) up until the end of the string ($). See if there are some solutions from other people, as I say there's often a better way :).
 
Last edited:
Just as an extra note, this is for a "text in file" search in eclipse (project with 1200+ php files), not for use in a script (makes no difference to the regex but maybe there's a better way?).

I've tried using

class/helper !class/helper/mysql

and various combinations thereof, none of which seem to work with the standard search, which is why I was trying to do it using regex.
 
without knowing the full requirements it's hard to know exactly what you need.. but if you explicity just want anything but mysql after it, the following pattern would work:
Code:
^class/helper/[^(mysql)]+$
 
Just to be clear, these strings could appear anywhere in the file, usually inside a string literal.

i.e.
Code:
require_once(CLASS_PATH."class/helper/userhelper.class.php");

to become 

require_once(CLASS_PATH."class/helper/mysql/userhelper.class.php");

The reason I'm searching for them is because I have to modify the directory structure to include support for multiple RDBs and I think I missed a few the first time round when I did the search-replace.
 
Back
Top Bottom