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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Perl Regular Expression question

  • 29-11-2007 9: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