Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Perl Regular Expression question

  • 29-11-2007 09: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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