sed 中的正则表达式用于在一个语句中进行多次替换 [英] Regular Expression in sed for multiple replacements in one statement
问题描述
我想清理一些输入并用可接受的输入替换几个字符,例如带有aa
"的丹麦语å
".
I want to sanitise some input and replace several characters with acceptable input, e.g. a Danish 'å
' with 'aa
'.
这可以使用多个语句轻松完成,例如/æ/ae/
、/å/aa/
、/ø/oe/
,但由于工具限制,我希望能够在单个正则表达式中执行此操作.
This is easily done using several statements, e.g. /æ/ae/
, /å/aa/
, /ø/oe/
, but due to tool limitations, I want to be able to do this in a single regular expression.
我可以捕获所有相关案例 (/[(æ)(ø)(å)(Æ)(Ø)(Å)]/
) 但我替换不起作用,因为我希望它(但可能完全按照预期):
I can catch all of the relevant cases (/[(æ)(ø)(å)(Æ)(Ø)(Å)]/
) but I replacement does not work as I want it to (but probably completely as intended):
$ temp="RødgrØd med flæsk"
$ echo $temp
RødgrØd med flæsk
$ echo $temp | sed 's/[(æ)(ø)(å)(Æ)(Ø)(Å)]/(ae)(oe)(aa)(Ae)(Oe)(Aa)/g'
R(ae)(oe)(aa)(Ae)(Oe)(Aa)dgr(ae)(oe)(aa)(Ae)(Oe)(Aa)d med fl(ae)(oe)(aa)(Ae)(Oe)(Aa)sk
(第一行回声是为了表明它不是编码问题)
(first echo line is to show that it isn't an encoding issue)
顺便说一句,工具问题是我还想在 Sublime Text 2 片段.
Just as an aside, the tool issue is that I should like to also use the same regex in a Sublime Text 2 snippet.
有人能看出我的正则表达式有什么问题吗?
Anyone able to discern what is wrong with my regex statement?
提前致谢.
推荐答案
把它拆分成几个 sed
语句,用 ;
分隔:
Split it up into several sed
statements, separated by ;
:
sed 's/æ/ae/g;s/ø/oe/g;s/å/aa/g;s/Æ/Ae/g;s/Ø/Oe/g;s/Å/Aa/g'
这篇关于sed 中的正则表达式用于在一个语句中进行多次替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!