首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Perl到Python的Regex转换

从Perl到Python的Regex转换
EN

Stack Overflow用户
提问于 2014-01-30 14:45:35
回答 2查看 1.3K关注 0票数 2

我想将一个小型Perl程序重写为Python。我正在按以下方式处理文本文件:

输入:

代码语言:javascript
复制
00000001;Root;;
00000002;  Documents;;
00000003;    oracle-advanced_plsql.zip;file;
00000004;  Public;;
00000005;  backup;;
00000006;    20110323-JM-F.7z.001;file;
00000007;    20110426-JM-F.7z.001;file;
00000008;    20110603-JM-F.7z.001;file;
00000009;    20110701-JM-F-via-summer_school;;
00000010;      20110701-JM-F-yyy.7z.001;file;

期望产出:

代码语言:javascript
复制
00000001;;Root;;
00000002;  ;Documents;;
00000003;    ;oracle-advanced_plsql.zip;file;
00000004;  ;Public;;
00000005;  ;backup;;
00000006;    ;20110323-JM-F.7z.001;file;
00000007;    ;20110426-JM-F.7z.001;file;
00000008;    ;20110603-JM-F.7z.001;file;
00000009;    ;20110701-JM-F-via-summer_school;;
00000010;      ;20110701-JM-F-yyy.7z.001;file;

下面是工作的Perl代码:

代码语言:javascript
复制
#filename: perl_regex.pl
#/usr/bin/perl -w
while(<>) {                                                           
  s/^(.*?;.*?)(\w)/$1;$2/;                                            
  print $_;                                                           
}      

它从命令行调用它:perl_regex.pl input.txt

对Perl样式regex的解释:

代码语言:javascript
复制
s/        # start search-and-replace regexp
  ^       # start at the beginning of this line
  (       # save the matched characters until ')' in $1
    .*?;  # go forward until finding the first semicolon
    .*?   # go forward until finding... (to be continued below)
  )
  (       # save the matched characters until ')' in $2
    \w    # ... the next alphanumeric character.
  )
/         # continue with the replace part
  $1;$2   # write all characters found above, but insert a ; before $2
/         # finish the search-and-replace regexp.

有人能告诉我,如何在Python中获得相同的结果吗?特别是对于$1和$2变量,我找不到相似的东西。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-30 14:52:23

Python正则表达式非常类似于Perl的表达式,除了:

  • 在Python中没有正则表达式文字。它应该用字符串来表示。我在下面的代码中使用了r'raw string literal'
  • 反向引用表示为\1\2、.或者\g<1>\g<2>,。
  • ..。

使用re.sub替换。

代码语言:javascript
复制
import re
import sys

for line in sys.stdin: # Explicitly iterate standard input line by line
    # `line` contains trailing newline!
    line = re.sub(r'^(.*?;.*?)(\w)', r'\1;\2', line)
    #print(line) # This print trailing newline
    sys.stdout.write(line) # Print the replaced string back.
票数 1
EN

Stack Overflow用户

发布于 2014-01-30 14:52:15

python中的替换指令是re.sub(模式,替换,字符串)函数或re.compile(模式).sub(替换,字符串)。在你的情况下,你会这样做:

代码语言:javascript
复制
_re_pattern = re.compile(r"^(.*?;.*?)(\w)")
result = _re_pattern.sub(r"\1;\2", line)

注意,$1变成了\1。至于perl,您需要以自己想要的方式迭代行(打开、输入文件、拆分行、.)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21459563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档