Regex

Discussion in 'PHP' started by andytan91, Jul 26, 2010.

  1. #1
    Hello guys i want to replace the value at the "0x1" part from this three findstr function in a file. I have tried doing it for an hour but i still can't do it so i need a lil help...thanks

    findstr /i /C:"0x1" C:\reg1.txt
    findstr /i /C:"0x1" C:\reg2.txt
    findstr /i /C:"0x1" C:\reg3.txt

    This is what i tried below
    $batch_contents = preg_replace('~"([a-z0-9_ -]+)"\sC:\reg1.txt ~i', '$newArgument.\sC:\reg1.txt', $batch_contents);

    $batch_contents = preg_replace('~"([a-z0-9_ -]+)"\sC:\reg2.txt ~i', '$newArgument.\sC:\reg2.txt', $batch_contents);
     
    andytan91, Jul 26, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    If its a constant 0x1 then str_replace might be easier. Can you post what you have exactly and your desired result, your above post is a little confusing.
     
    MyVodaFone, Jul 26, 2010 IP
  3. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Basically i'm doing an edit function
    i want this line -> findstr /i /C:"0x1" C:\reg1.txt
    to be changed to - > findstr /i /C:"0x2" C:\reg1.txt for example..
     
    andytan91, Jul 26, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If it's not a constant, something like this would suffice:

    
    <?php
        $string = 'findstr /i /C:"0x1" C:\reg1.txt';
        $replacement_text = 'replaced';
        
        $string = preg_replace('#(?<=findstr\s/i\s/C:")(.*?)(?="\sC:\\\\reg1.txt)#i', $replacement_text, $string);
        echo $string;
    ?>
    
    PHP:
     
    Deacalion, Jul 26, 2010 IP
  5. andytan91

    andytan91 Greenhorn

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Thanks alot, it works! Thank you everyone!

     
    andytan91, Jul 26, 2010 IP