views:

32

answers:

2

Hi, I have been trying to get this regex work. Suppose to parse an URL, if the string '_skipThis' is found, don't match the string. Also backreferences are needed too. For example:

String 1: a_particular_part_of_string/a/b/c/d/e/f
Result: preg_match should return true
Backreference: $1 -> a_particular_part_of_string, $2 -> /a/b/c/d/e/f

String 2: a_particular_part_of_string_skipThis/a/b/c/d/e/f
Result: preg_match should return false.
Backreference: nothing here.

I have tried the following regex..

reg1 = ([a-zA-Z0-9_]+)(\/.*)
reg2 = ([a-zA-Z0-9]+(?!_skipThis))(\/.*)
reg3 = ((?!_skipThis).*)(\/.*)
reg4 = ((?!_skipThis)[a-zA-Z0-9_]+)(\/.*)

Please help me! Thanks in advance!!!

A: 

Just match _skipThis and return false if it is found.

 if (strpos($theString, '_skipThis') === false) {
        // perform preg_match
 } else 
        return false;

(Of course there is a regex for this. Assuming _skipThis only appears before the first /,

 return preg_match('|^([^/]+)(?<!_skipThis)(/.*)$|', $theString); 
 //                   -------              -----
 //                     $1   --------------  $2
 //                          Make sure it is *not* preceded '_skipThis'

Otherwise, if the _skipThis appearing anywhere needs to be avoided,

 return preg_match('|^(?!.*_skipThis)([^/]+)(/.*)$|', $theString);
 //                   ---------------
 //                   Make sure '_skipThis' is not found anywhere. 
KennyTM
+1  A: 

Try this :

$str1 = 'a_particular_part_of_string/a/b/c/d/e/f'; //1
$str2 = 'a_particular_part_of_string_skipThis/a/b/c/d/e/f'; //0
$keep = 'a_particular_part_of_string';
$skip = '_skipThis';

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str1, $m))
    var_dump($m);
else
    echo "Not found\n";

$m = array();
if(preg_match("/($keep)(?!$skip)(.*)$/", $str2, $m))
    var_dump($m);
else
    echo "Not found\n";

Output:

array(3) {
  [0]=>
  string(39) "a_particular_part_of_string/a/b/c/d/e/f"
  [1]=>
  string(27) "a_particular_part_of_string"
  [2]=>
  string(12) "/a/b/c/d/e/f"
}

Not found
M42
Thanks a lot for your help. It is necessary for the 'a_particular_part_of_string' to be different all the time. Thanks!