SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    capture values from regex?

    Hey all. I'm pretty sure the answer to this is no, but I thought I'd better ask anyway in case there's a way I don't know about.

    In perl you can use variables that you've captured in a regex after the line that the regex is on.

    Example:

    if($strVar =~ /I (love) nachos/)
    { print "do you $1 nachos?";
    }

    Will print:
    do you love nachos?

    So is there a way to capture values like this in PHP and use them later?
    Or are you restricted to using them in the other arguments of the preg_* function?

    --ed

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about preg_grep?
    Erh

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sure...
    PHP Code:
    if(preg_match('/I (love) nachos/'$strVal$matches))
    { print 
    "do you {$matches[1]} nachos?";

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by sweatje
    sure...
    PHP Code:
    if(preg_match('/I (love) nachos/'$strVal$matches))
    { print 
    "do you {$matches[1]} nachos?";

    I couldn't count how many times I've done this.
    Momentary lapse of reason, I guess.

    --ed

  5. #5
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:

    Code:
    $regex = "I (love) Nachos";
    preg_prelace("#$regex#","Do you \$1 Nachos?");
    It's called backreferencing.
    http://uk2.php.net/manual/en/function.preg-replace.php

    HTH

  6. #6
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by phptek
    How about:

    Code:
    $regex = "I (love) Nachos";
    preg_prelace("#$regex#","Do you \$1 Nachos?");
    It's called backreferencing.
    http://uk2.php.net/manual/en/function.preg-replace.php

    HTH
    Yeah, even though I got confused when I asked my question, my original intent was to use captured values from a preg_replace, and use it afterwards.
    Somewhere along the lines I lost track of the problem and got confused.
    I have fuzzy moments

    BUt it'd still be nice to be able to use captured values from a preg_replace after the fact.

    --ed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •