why does this regular expression fail?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawrence k

    why does this regular expression fail?

    Suppose I have these five lines in a file somewhere:

    <p>This entry should belong to:
    <select name="formInput s[id_of_neighborh ood_to_which_th is_belongs]">
    <option value="">(No choice made)</option>
    <?php getDatabaseTabl eValuesInOption Tags("neighborh oods", "id",
    "state"); ?>
    </select>

    Suppose I open this file and read the contents into a string called
    $string. Suppose I then give the string to this function:


    function matchAllPhpFunc tionsInString($ subject=false) {
    // 11-08-06 - this is being called in importForm

    $pattern = "<";
    $pattern .= "\?php.*\(. *\); \?";
    $pattern .= ">";
    $pattern = "/$pattern/";
    preg_match_all( $pattern, $subject, $matches);
    // print_r($matche s);

    // 09-19-06 - there is no point returning a 2 dimensional array, so we
    will make
    // it one dimensional.
    $arrayOfPhpFunc tionNames = $matches[0];
    return $arrayOfPhpFunc tionNames;
    }



    This function is suppose to find the PHP command. It works on other
    pages, but not the one I've posted above. Why is that? Why would this
    regular expression not find this PHP command?

  • Juliette

    #2
    Re: why does this regular expression fail?

    lawrence k wrote:
    Suppose I have these five lines in a file somewhere:
    >
    <p>This entry should belong to:
    <select name="formInput s[id_of_neighborh ood_to_which_th is_belongs]">
    <option value="">(No choice made)</option>
    <?php getDatabaseTabl eValuesInOption Tags("neighborh oods", "id",
    "state"); ?>
    </select>
    >
    Suppose I open this file and read the contents into a string called
    $string. Suppose I then give the string to this function:
    >
    >
    function matchAllPhpFunc tionsInString($ subject=false) {
    // 11-08-06 - this is being called in importForm
    >
    $pattern = "<";
    $pattern .= "\?php.*\(. *\); \?";
    $pattern .= ">";
    $pattern = "/$pattern/";
    preg_match_all( $pattern, $subject, $matches);
    // print_r($matche s);
    >
    // 09-19-06 - there is no point returning a 2 dimensional array, so we
    will make
    // it one dimensional.
    $arrayOfPhpFunc tionNames = $matches[0];
    return $arrayOfPhpFunc tionNames;
    }
    >
    >
    >
    This function is suppose to find the PHP command. It works on other
    pages, but not the one I've posted above. Why is that? Why would this
    regular expression not find this PHP command?
    >

    In the pattern you ask for '; ?>', i.e. semi-colon-space-questionmark.

    In the code snippet you provided, there is more than one space behind
    the semi-colon, ergo: it is correct that the pattern does not match the
    code snippet.

    If more spaces are allowable, you can of course solve this by changing:
    $pattern .= "\?php.*\(. *\); \?";
    to:
    $pattern .= "\?php.*\(. *\); +\?";

    Comment

    • lawrence k

      #3
      Re: why does this regular expression fail?


      Juliette wrote:
      lawrence k wrote:
      Suppose I have these five lines in a file somewhere:

      <p>This entry should belong to:
      <select name="formInput s[id_of_neighborh ood_to_which_th is_belongs]">
      <option value="">(No choice made)</option>
      <?php getDatabaseTabl eValuesInOption Tags("neighborh oods", "id",
      "state"); ?>
      </select>

      Suppose I open this file and read the contents into a string called
      $string. Suppose I then give the string to this function:


      function matchAllPhpFunc tionsInString($ subject=false) {
      // 11-08-06 - this is being called in importForm

      $pattern = "<";
      $pattern .= "\?php.*\(. *\); \?";
      $pattern .= ">";
      $pattern = "/$pattern/";
      preg_match_all( $pattern, $subject, $matches);
      // print_r($matche s);

      // 09-19-06 - there is no point returning a 2 dimensional array, so we
      will make
      // it one dimensional.
      $arrayOfPhpFunc tionNames = $matches[0];
      return $arrayOfPhpFunc tionNames;
      }
      >
      >
      In the pattern you ask for '; ?>', i.e. semi-colon-space-questionmark.
      >
      In the code snippet you provided, there is more than one space behind
      the semi-colon, ergo: it is correct that the pattern does not match the
      code snippet.
      Thanks so much! It's funny, I stared at that for a long time, and never
      noticed the extra space!

      Comment

      Working...