-5

I need regular expression to search for comma if it appears more than once inside || and || and return the string between || and ||. For eg. consider this string:

$str = "http://www.test.com||abd-asd,asf,asfdsf[asf[s]asf,http://www.test1.com||asfsaf";

so after running regular expression on above string, it should return:

abd-asd,asf,asfdsf[asf[s]asf,http://www.test1.com

excluding || and ||. I have to use this in my PHP code.

codelearner
  • 1,354
  • 1
  • 16
  • 32

1 Answers1

1
if (preg_match(
    '/(?<=\|\|) # Assert starting position directly after ||<
    [^,|]*,     # Match any number of characters except , or |; then a ,
    [^,|]*,     # twice (so we have matched at least two commas)
    [^|]*       # Then match anything except | characters
    (?=\|\|)    # until we are right before ||
    /x', 
    $subject, $regs)) {
    $result = $regs[0];
} else {
    $result = "";
}
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561