#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Regex for replacing the first word of a string from - to _


    below is my string and I am using to convert variable theline first word with - to _.

    $theline =~ s/-^/_/g;

    my $theline = Real-Blockset ("70" , "Aerospace Blockset" , " Aerospace_Blockset", "AE", "aeroblks"),
    real_Toolbox ("108" , "Aerospace Toolbox" , " Aerospace_Toolbox", "AT", "aero"),


    but my above regex is not changing the - to _.


    Please help !!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    831
    Rep Power
    496
    Remove the circumflex accent (^) from the regex.

    Code:
    $theline =~ s/-/_/g;
    The ^ assertion means start of string (or start of line in some specific contexts). Therefore /-^/ cannot match anything.

    Comments on this post

    • vaibhav.shukla agrees : Thanks !!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    831
    Rep Power
    496
    If it should be only in the first word, something like this:

    Code:
    $theline =~ s/^(\d+)-/$1_/g;
    I had not seen that part of the requirement and did not have time to come back until now.

IMN logo majestic logo threadwatch logo seochat tools logo