【问题标题】:perl code to find and substitute a patternperl code to search and replace a pattern
【发布时间】:2022-12-02 01:09:03
【问题描述】:
output        [15:0] pin;                
output         [1:0] en;                
input          [6:0] dddr;            
input          [6:0] dbg;

replace this with ( I am counting the bus)

16 : pin : output;                         
2 : en : output;                
7 : dddr : input;            
7 : dbg :input;

I tried this code after opening the file and stored it in var. but i am not able to filter it like above

if ($var =~ /(\w+)\[(\d+)\:/) {  
    print "word=$1 number=$2\n";
}

//i am trying to add : in middle of the columns also

【问题讨论】:

  • You are missing the whitespace.

标签: regex perl grep


【解决方案1】:

You are missing the whitespace after the word characters in your pattern.

(w+ )       [(d+):
      VVVVVVVV
output        [15:0] pin;                

This is easily fixed. Add it into the pattern in between, like so:

use strict;
use warnings;
use feature 'say';

while (my $line = <DATA>) {
    if ($line =~ /(w+)s+[(d+):/) {
        say "word=$1 number=$2";
    }
}

__DATA__
output        [15:0] pin;
output         [1:0] en;
input          [6:0] dddr;
input          [6:0] dbg;

This produces:

word=output number=15
word=output number=1
word=input number=6
word=input number=6

To get to your desired output, you'll have to refine the pattern and probably do some incrementing too.

【讨论】:

    【解决方案2】:
    while (<>) { 
        say $2+1, ' : ', $3, ' : ', $1, ';'  if /(S+)s+[(d):d]s+(w+)/
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-28
      • 2022-12-02
      • 2019-12-25
      • 2022-12-26
      • 1970-01-01
      相关资源
      最近更新 更多