正则表达式替换:'[AZ]''到[AZ]' - 我无法保留字符串中的字母
我的 google foo 失败了...
我有一个文件(超过 2 GB)存在 SQL 格式问题。所以我需要一个正则表达式来更新以下示例(记住,我不知道有多少个或字母是什么):
- 'N'' 应该更改为 N'
- 'L'' 应该更改为 L' 等等
我已经尝试过(在 VIM 和 sed 中):
s/'[A-Z]''/$1'/
但这只会产生:
'N'' -> '$1'
My google foo is failing me...
I have a file (well over 2 gig's) that has a SQL format problem. So I need a regex that will update the following examples (remember, I don't know how many there are or what the letters are):
- 'N'' should be changed to N'
- 'L'' should be changed to L'
etc
I've tried (within VIM and sed):
s/'[A-Z]''/$1'/
but that just produces:
'N'' -> '$1'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sed 中的反向引用是
\1
,而不是$1
。您还需要使用\(\)
捕获该字母(并且可能使用全局标志g
)。你的 sed 表达式应该是:
A backreference in sed is
\1
, not$1
. You also need to capture the letter using\(\)
(and probably use the global flagg
).Your sed expression should be:
试一试:
示例输出
注意:既然你说你不知道它们是什么字母,我假设它们可能是小写的。如果您确实知道它们始终为大写,请将
[[:alpha:]]
更改为[[:upper:]]
。这些字符类分别优于[A-Za-z]
和[AZ]
,因为无论语言环境如何,它们都将始终按您的预期工作。Give this a shot:
Example Output
Note: Since you said you didn't know what letters they would be I assumed they could be lower case. If you know for a fact they are always uppercase, then change
[[:alpha:]]
to[[:upper:]]
. These character classes are preferred over[A-Za-z]
and[A-Z]
, respectively, because they will always work as you expect no matter the locale.