多个char替换为正则表达式

时间:2015-08-04 06:18:25

标签: regex sublimetext2 sublimetext

需要用regexp替换大量的char:

  before -->  h e l l o
              | | | | |
  result -->  a b c c d

如何? :)

实际上,需要在html中将所有字符Unicode(UTF-8)替换为ASCII(Unicode Escaped)这个问题只是一个简化的例子

UPD

好吧,总是忘记可以通过正则表达式搜索文本但不能替换,问题已解决,谢谢

1 个答案:

答案 0 :(得分:1)

这应该符合您的目的。我把它保存在utf-to-ascii.py中。

#!/usr/bin/env python

import sys

for c in sys.stdin.read().decode('UTF-8'):
    charcode = ord(c)
    if charcode > 127:
        sys.stdout.write('\\u%04x'%(charcode))
    else:
        sys.stdout.write(c)

我用一个名为textdoc.txt的文件测试了这些内容:

hello ד blah blah
我是这样跑的:

$ ./utf-to-ascii.py <textdoc.txt 
hello \u05d3 blah blah

要将该输出保存到文件,请执行以下操作:

$ ./utf-to-ascii.py < textdoc.txt > textdoc.transformed.txt