简体   繁体   English

Perl正则表达式的Python版本

[英]Python version of a Perl regular expression

I have this Perl regular expression and I want to convert it to Python. 我有这个Perl正则表达式,我想将其转换为Python。

The regex I want is a search and replace that finds text and converts it to upper case. 我想要的正则表达式是一个搜索和替换,可查找文本并将其转换为大写。 It also must be the first occurring result. 它也必须是第一个出现的结果。 Perl regex: Perl正则表达式:

open FILE, "C:/thefile.txt";
while (<FILE>){
    # Converts "foo yadayada bar yadayada"
    #       to "FOO  bar yadayada"
    s/(^.*?)(yadayada)/\U$1/;
    print;
}

The Python regex I have is not working correctly: 我拥有的Python正则表达式无法正常工作:

import re
lines = open('C:\thefile.txt','r').readlines()
for line in lines:
    line = re.sub(r"(yadayada)","\U\g<1>", line, 1)
    print line

I realize the \\U\\g<1> is what isn't working because Python doesn't support \\U for uppercase.. so what do I use!?! 我意识到\\U\\g<1>无法正常工作,因为Python不支持大写的\\U ..所以我该怎么用!!!

re.sub can take a function, which processes each match object and returns a string. re.sub可以采用一个函数,该函数处理每个匹配对象并返回一个字符串。 So you can do it like this: 因此,您可以这样做:

In [4]: def uppergrp(match):
   ...:     return match.group(1).upper()
   ...: 

In [5]: re.sub("(yada)", uppergrp, "abcyadadef", count=1)
Out[5]: 'abcYADAdef'

Working with regexes in Python is less convenient, but Python programmers tend to be less keen to use regexes than Perl coders. 在Python中使用正则表达式不太方便,但是与Perl编码器相比,Python程序员不太喜欢使用正则表达式。

The second argument to sub can also be a function, meaning if regex language in python cannot accomplish what you want (or at least makes it very difficult) you can just define your own function to use instead. sub的第二个参数也可以是一个函数,这意味着如果python中的正则表达式语言无法完成您想要的(或至少使其非常困​​难),则可以定义自己的函数来使用。

eg. 例如。

re.sub(pattern, lambda x: x.group(1).upper(), string)

edit: The function gets passed a MatchObject 编辑:函数被传递一个MatchObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM