Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换Python中字符串中的未知字母数字字符_Python_Regex_String_Python 2.7 - Fatal编程技术网

替换Python中字符串中的未知字母数字字符

替换Python中字符串中的未知字母数字字符,python,regex,string,python-2.7,Python,Regex,String,Python 2.7,我有字符串的以下方面:- “a*.b*” “*”表示一个未知的字母数字字符,因为该数据正在被刮取,因此字符串的方面可以是“a1.b1”或“az.bz”。同样,这会出现在较长的字符串中,因此可能会出现在: "http://www.a*.b*.com" where '*' = '1'. 字符串的长度未知 我想做的是,每次在字符串中遇到模式“a*.b*”(其中“*”是字母数字字符,我希望字母“a”替换为“z”),因此:- “a1.b1”变为“z1.b1”,“a4.b1”变为“z4.b1” 如上所述

我有字符串的以下方面:-

“a*.b*”

“*”表示一个未知的字母数字字符,因为该数据正在被刮取,因此字符串的方面可以是“a1.b1”或“az.bz”。同样,这会出现在较长的字符串中,因此可能会出现在:

"http://www.a*.b*.com" where '*' = '1'. 
字符串的长度未知

我想做的是,每次在字符串中遇到模式“a*.b*”(其中“*”是字母数字字符,我希望字母“a”替换为“z”),因此:-

“a1.b1”变为“z1.b1”,“a4.b1”变为“z4.b1”

如上所述,上面的功能在一个字符串中

有什么建议吗?

试试这个:

lst = ["http://www.a3.bs.com",
       "http://www.a1.bw.com",
       "http://www.a2.be.com",
       "http://www.a3.br.com"]

for i in re.findall("http://www\.a[0-9a-zA-Z]\.b[0-9a-zA-Z]\.com", " ".join(lst)):
    print re.subn('a', 'z', i)[0]

你研究过Python正则表达式并提出了一个尝试性的解决方案了吗?我会看一看,谢谢Elzion。我一直在使用replace函数,但只能在定义的部分字符串上运行它,该字符串包含已知字符,而不是遵循模式的未知字符