Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Regular Expression question

Options
  • 29-11-2007 10:03pm
    #1
    Closed Accounts Posts: 334 ✭✭


    Hi,

    This is probably basic enough, but I can't figure it out. I want to do a string replacement in Perl.

    I have a file with the following line:

    IPAdd = 1xx.xx.xx.xx

    and I want to replace it with

    IPAdd = 1yy.yy.yy.yy

    However, IPAdd 1xx.xx.xx.xx could be any IP Address, so I am searching for IPAdd and I want to replace the entire line with IPAdd = 1yy.yy.yy.yy

    What I have so far is:
    my $text;
    my $string;
    my $replace;
    
    $string = "IPAdd";
    $replace = "IPAdd = 1yy.yy.yy.yy";
    $text =~ s/$string/$replace/g;
    
    

    However, this writes the line IPAdd = 1yy.yy.yy.yy 1xx.xx.xx.xx
    Can anyone please point me in the right direction for this?
    To sum it up, I want to search for part of a string and replace that string and all of that strings line with a new string.

    Thanks.


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt


    This may sound bad but have you actually gone through the documentation on regular expressions ?


  • Closed Accounts Posts: 334 ✭✭WhatsGoingOn


    forbairt wrote: »
    This may sound bad but have you actually gone through the documentation on regular expressions ?

    I have, and I know I am missing something obvious, but it is wrecking my head:(


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    ok sorry ... are xx and yy specific numbers that you know ?


  • Closed Accounts Posts: 334 ✭✭WhatsGoingOn


    forbairt wrote: »
    ok sorry ... are xx and yy specific numbers that you know ?
    I will know what yy is, but not what xx is


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    In that case your string I think should be
    $string = "IPAdd = 1\d\d\.\d\d\.\d\d\.\d\d";
    

    the \d = number
    the \. makes sure its a dot and not a special character


  • Advertisement
  • Closed Accounts Posts: 334 ✭✭WhatsGoingOn


    forbairt wrote: »
    In that case your string I think should be
    $string = "IPAdd = 1\d\d\.\d\d\.\d\d\.\d\d";
    

    the \d = number
    the \. makes sure its a dot and not a special character
    The only problem with that is that I am searching for an IP address, and it may not always be in the format above.
    e.g. it could be 111.222.33.44 or 111.222.333.44 etc.

    It's ok, I'll figure it out, cheers.


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    $string = "IPAdd = 1\d+\.\d+\.\d+\.\d+";
    

    adding a + should do the trick sorry
    I normally keep things looking as simple as possiblee where regexp is concerned so when I have to go back over it I know whats happening

    \d+ will match 1 or more numbers


  • Closed Accounts Posts: 334 ✭✭WhatsGoingOn


    Works perfectly, thanks forbairt


Advertisement