Outils pour utilisateurs

Outils du site


web:php:regex:example

Ceci est une ancienne révision du document !


Table des matières

Exemples de regex en vrac

  // 1- remove recursively the content of for (), [] and {}
  // 2- removes escaped ' and "
  // 3- removes content betweend '' and "" (no recursion needed)
  $embracerPattern = "/\[([^\[\]]++|(?R))*+\]|\(([^\(\)]++|(?R))*+\)|{([^{}]++|(?R))*+}/";
  $escapedQuotePattern = "/\\\\'|\\\\\"/";
  $quotedStringPattern = "/'([^']++)*+'|\"([^\"]++)*+\"/";
  //replace escaped quotes with html entities
  $escapedQuotePattern = "/\\\\'/";
  $call['code'] = preg_replace($escapedQuotePattern, '\&#apos;', $call['code']);
  $escapedQuotePattern = "/\\\\\"/";
  $call['code'] = preg_replace($escapedQuotePattern, '\&#quot;', $call['code']);
  
  //replace commas between remaining (double) quotes with html entities
  $quotedStringPattern = "/'([^']++)*+'|\"([^\"]++)*+\"/";
  $call['code'] = preg_replace_callback(
      $quotedStringPattern,
      function ($matches) {
          return str_replace(',',',',$matches[0]);
      },
      $call['code']
  );
  
  //replace commas between parentheses, braces or brackets
  $embracerPattern = "/\[([^\[\]]++|(?R))*+\]|\(([^\(\)]++|(?R))*+\)|{([^{}]++|(?R))*+}/";
  $call['code'] = preg_replace($embracerPattern, '', $call['code']);
  
  //get the list of given parameters
  $results = preg_split('/,/', $call['code']);
web/php/regex/example.1381411760.txt.gz · Dernière modification : le 10/10/2013 à 13:29 de Yosko