Perl Search and Replace RegEx Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anda007
    New Member
    • Aug 2010
    • 5

    Perl Search and Replace RegEx Question

    Hi,
    I have one more question ..
    I have a line which looks like this ..

    (`CHN_WSM.CG_AB C_CLK)
    In the same file, there is a another line which looks like this
    (`CHN_ABC.SOB_A BC_DATA_E),

    I need to put a search and replace syntax in perl, so that the string "ABC" after the "." is replaced by XYZ ..
    Which means , the output file will have the following two strings

    (`CHN_WSM.CG_XY Z_CLK),
    (`CHN_ABC.SOB_X YZ_DATA_E),

    I used this syntax -- for that ..
    $inp_line[1] =~ s/\.(.*ABC)/\.XYZ/;
    But this gave me an output of --

    (`CHN_WSM.XYZ_C LK),
    (`CHN_ABC.XYZ_D ATA_E),

    How do I maintain the original line and just insert the replacement .. ?
    Anand
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    Are you parsing Verilog by any chance?

    Code:
    use warnings;
    use strict;
    
    while (<DATA>) {
        s/\.(.*?)ABC/.${1}XYZ/;
        print;
    }
    
    __DATA__
    (`CHN_WSM.CG_ABC_CLK)
    (`CHN_ABC.SOB_ABC_DATA_E),
    Prints out:

    Code:
    (`CHN_WSM.CG_XYZ_CLK)
    (`CHN_ABC.SOB_XYZ_DATA_E),

    Comment

    • anda007
      New Member
      • Aug 2010
      • 5

      #3
      Hey, thanks again for that ..
      Yes, I am working on parsing few Verilog and System Verilog files ..

      Comment

      • anda007
        New Member
        • Aug 2010
        • 5

        #4
        s/\.(.*?)ABC/.${1}XYZ/;

        Hey, small change in the code you sent ..
        The $1 does have the { }

        The code should look ..
        s/\.(.*?)ABC/.$1XYZ/;

        Minor stuff.. Thanks again for all your help
        Anand

        Comment

        • toolic
          Recognized Expert New Member
          • Sep 2009
          • 70

          #5
          It works with or without the curly braces, but I think it is easier to understand with the braces to separate the 1 from the ABC.

          Comment

          Working...