2017-02-17 71 views
1

在随机字符串中,我需要找到一个匹配给定模式的字符串,并在此字符串后面加上;。我认为我应该使用re来做到这一点,但我并不熟悉它。找到一个匹配给定模式的字符串,并使用Python的re模块分隔线

例输入:

this is the first part of string 1/32 part this is the second part of string 

结果,我需要把;1/32 part后,如

this is the first part of string 1/32 part; this is the second part of string 

我知道我应该用re,我知道我可能应该使用re.match与看起来像[1-1000]/[1-1000]\spart模式,但我不知道从哪里去。

编辑:1/32就是一个例子,它可以65/1231/36/7

+0

为什么'[1-1000]'?你确切的要求是什么?如果有'/'还有没有关系? –

+5

注意'[1-1000]'是一个*字符组*,因此只会匹配'0'和'1'... –

+0

您只需要匹配'\ d +/\ d + \ s + part' – anubhava

回答

4

你的用例被称为替代。这正是re.sub功能的用途。

import re 

s = "bla 1/6 part bla bla 76/88 part 12345/12345 part bla" 
print(s) 
s = re.sub(r'(\b\d{1,4}/\d{1,4} part)', r'\1;', s) 
print(s) 

的这个输出是

bla 1/6 part; bla bla 76/88 part; 12345/12345 part bla 

part最后一次发生后,失踪;

我用{} quantifiers限制分数的分子和分母为4个十进制数字,这是你提到的[1-1000]表示法。它可以更好地近似为1?\d{1,3}(但是这也不完全相同,它也允许例如1999/1999[1]


[1] P.S.tripleee commented一样,十进制数的范围从1到1000的确切正则表达式是[1-9]([0-9][0-9]?)?|1000,它看起来有点复杂,但如果您将唯一的4位数字1000分开,并使用多余的一对圆括号1至3位数字部分:[1-9]([0-9]([0-9])?)?。另一个选项是对[0-9]使用字符类快捷键\d,从而产生[1-9]\d{0,2}|1000

编辑:

  • 相结合的比赛分组。
  • 在分子之前添加了锚点。
+0

谢谢。解决了我的问题:) – krizz

4

您只需要使用re.matchre.subre模块,用下面的正则表达式

import re 

my_str = 'this is the first part of string 1/32 part this is the second part of string' 
my_regex = r'(\d+/\d+\s+part)' 

if re.match(my_regex, my_str): 
    print(re.sub(my_regex, r'\1,', my_str)) # this will print: 1/32 part, 
    # ... 

裸沿如果需要多行来匹配相同的正则表达式,则需要向正则表达式添加一些额外的标志。请参阅here此类标志的列表。

你可以看到正则表达式here


快速更换(有可能是更好的方式)将也部分之前和所需的匹配部分匹配后,做一些事情,如:

import re 

my_str = 'this is the first part of string 1/32 part this is the second part of string' 
my_regex = r'(.*)(\s+\d+/\d+\s+part)(.*)' 

condition = re.match(my_regex, my_str) 

if condition: 
    part = re.sub(my_regex, r'\2,', my_str) 

x = condition.group(1) + part + condition.group(3) 
print(x) 

将输出修改后的字符串:

这是串1/32的第一部分某种程度上,这是 串的第二部分

与所有的一个简单的联机功能上面会:

import re 


def modify_string(my_str, my_regex): 
    return re.sub(my_regex, r'\1,', my_str) 

if __name__ == '__main__': 
    print(modify_string('first part of string 1/32 part second part of string', r'(\d+/\d+\s+part)')) 

但我建议你保持状态。 以防万一

+0

如何在my_str中替换它,所以它在'part'之后包含','? – krizz

相关问题