2009-08-31 21 views
1

如何将下列匹配转换为给定结果?如何使用Perl重新排列此模板中项目的顺序?

我的文件有以下比赛

-- cut -- 
Lorem ipsun Hello lorem ipsun { $hello } 
@param integer $question_id     // example of the match 
Lorem ipsun Hello lorem ipsun { $hello } 
-- cut -- 

我想有效地改变他们

@param $question_id integer 

我的伪代码尝试

perl -i.bak -pe `s/(@param) (\\w) ($\\w)/$1 $3 $2/` 
+1

代表您填写的参数或字符串“@参数”的文本中的@符号,同一问题与$ -sign by $ question-id – 2009-08-31 15:30:44

+0

@Commusoft:@参数是问题中的一个符号类似于$ in $ question_id – hhh 2009-08-31 15:45:46

回答

5

你的意思是(假设bash shell):

perl -i.bak -pe 's/(\@param) (\w+) (\$\w+)/$1 $3 $2/' 
+0

非常感谢!你为我节省了一天的生产时间:) – hhh 2009-08-31 16:09:00

1
s/(\@param)\s+(\w+)\s+(\$\w+)/$1 $3 $2/g 
2

,我可能要么去非常通用:

perl -i.bak -pe 's/^(\@param)(\s+)(\S+)(\s+)(\S+)/$1$2$5$4$3/' 

或非常具体:

perl -i.bak -pe 's/^(\@param)(\s+)(\$[_a-zA-Z]\w*)(\s+)(integer|long|char)(\s+)$/$1$2$5$4$3$6/' 

取决于数据。需要注意的是shell中$ identifier的字符串插值以及Perl中的@param$identifier的字符串插值。第一个通过在shell上使用单引号(以防止插值)处理,第二个通过转义@$(或使用避免必须明确地匹配$)的字符类来处理。

相关问题