如何使用Perl重新排列此模板中项目的顺序?

如何使用Perl重新排列此模板中项目的顺序?,perl,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 我在伪代码

如何将以下匹配项转换为给定结果?

我的文件具有以下匹配项

-- 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/`
您的意思是(假设bashshell):

您的意思是(假设bashshell):


我可能会选择非常普通的方式:

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上使用单引号来处理的(以防止插值),第二个是通过转义
@
$
(或使用避免显式匹配
$
的字符类)来处理的

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上使用单引号来处理的(以防止插值),第二个是通过转义
@
$
(或使用避免显式匹配
$
的字符类)来处理的。

代表您填写的参数或字符串“@param”的文本中的@-符号,相同的问题,用$签名,用$问题-id@Commusoft:@param是问题中的符号,与$in$question_id中的$in$sign类似,它代表您填写的参数或字符串“@param”的文本中的@符号,与$sign by$question相同-id@Commusoft:@param是问题中的一个符号,类似于$in$question_id谢谢!你刚刚为我节省了一天的生产时间:)非常感谢!您刚刚为我节省了一天的生产时间:)