How do I get this value from a regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    How do I get this value from a regular expression

    Hi,

    I'm having trouble with preg_match. I want to get the value in
    parenthese from this pattern

    $pattern = "/ab(.*)cde/";
    $str = "abxyzcde";

    What preg_match expression would i have to write to get the value "xyz"
    into a string?

    I'm using php 4.4.4. Thanks, - Dave

  • Moot

    #2
    Re: How do I get this value from a regular expression

    laredotornado@z ipmail.com wrote:
    Hi,
    >
    I'm having trouble with preg_match. I want to get the value in
    parenthese from this pattern
    >
    $pattern = "/ab(.*)cde/";
    $str = "abxyzcde";
    >
    What preg_match expression would i have to write to get the value "xyz"
    into a string?
    >
    I'm using php 4.4.4. Thanks, - Dave
    $pattern = "/(?<=ab)(.*)(?=c de)/";
    $str = "abxyzcde";
    preg_match($pat tern,$str,$matc h);
    var_dump($match );

    For documentation, look under the "Assertions " heading here:


    - Moot

    Comment

    • Michael Fesser

      #3
      Re: How do I get this value from a regular expression

      ..oO(Moot)
      >laredotornado@ zipmail.com wrote:
      >>
      >I'm having trouble with preg_match. I want to get the value in
      >parenthese from this pattern
      >>
      >$pattern = "/ab(.*)cde/";
      >$str = "abxyzcde";
      >>
      >What preg_match expression would i have to write to get the value "xyz"
      >into a string?
      >>
      >I'm using php 4.4.4. Thanks, - Dave
      >
      >$pattern = "/(?<=ab)(.*)(?=c de)/";
      >$str = "abxyzcde";
      >preg_match($pa ttern,$str,$mat ch);
      >var_dump($matc h);
      No need for assertions here. Just use the simple pattern from above and
      you can find the result in $match[1].

      Micha

      Comment

      Working...