如何将以下匹配转换为给定结果?
我的文件有以下匹配
-- 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/`
答案 0 :(得分:5)
你的意思是(假设是bash shell):
perl -i.bak -pe 's/(\@param) (\w+) (\$\w+)/$1 $3 $2/'
答案 1 :(得分: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上使用单引号来处理(以防止插值),第二个是通过转义@
和$
(或使用避免必须显式匹配{的字符类来处理的。 {1}})。
答案 2 :(得分:1)
s/(\@param)\s+(\w+)\s+(\$\w+)/$1 $3 $2/g