开发者

how to get the two '111 = ' using python Regular Expressions

this is mu\y code:

a = '111 = dw11qdwq\n111 = aaaaa'

print re.search(r'111 = (\S*)',a).group(1)

it show : dw11qdwq

but i want get dw11qdwq a开发者_如何学编程nd aaaaa

so what can i do ,

thanks

updated

a = '111 = dw11qdwq\n111 = aaaaa'

b=re.findall(r'111 = (\S*)',a)

d = ['qqqqqq','wwwwwww']
a= re.sub(r'111 = (\S*)',lambda x: d[],a)

and how to replace the dw11qdwq to qqqqqq ,aaaaa to wwwwwww

i want to get '111 = qqqqqq\n111 = wwwwwww'

updated:

it is ok now :

d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}

a = '111 = dw11qdwq\n111 = aaaaa'

print repr(a)
a= re.sub('(111\s*=\s*)(\S*)',lambda mat: mat.group(1)+d[mat.group(2)],a)
print repr(a)


a = '111 = dw11qdwq\n111 = aaaaa'

print re.findall(r'111 = (\S+)',a)

This matches any word with 1 or more non-space characters. (I'm fairly sure you don't want to match 0 or more characters(which is what '*' provide))

Edit:

Code for your update:

replacements = {"dw11qdwq":"qqqqqq" , "aaaaa" : "wwwwwww"}
a = '111 = dw11qdwq\n111 = aaaaa'


for key, value in replacements.iteritems():
  a = a.replace(key, value)

a is now:

"111 = qqqqqq\n111 = wwwwwww"


import re

d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}

a = '111 = dw11qdwq\n111 = aaaaa'

print repr(a)
a= re.sub('111\s*=\s*(\S*)',lambda mat: d[mat.group(1)],a)
print repr(a)

.

EDIT

Excuse me, I forgot to define 111\s*=\s* as a group You can do either

import re

d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}

a = '111 = dw11qdwq\n111 = aaaaa'

print repr(a)
a= re.sub('(111\s*=\s*)(\S*)',lambda mat: mat.group(1) + d[mat.group(2)],a)
print repr(a)

or

import re

def rpl(mat, d = {'dw11qdwq':'qqqqqq' ,'aaaaa':'wwwwwww'}):
    return mat.group(1) + d[mat.group(2)]

pat = re.compile('(111\s*=\s*)(\S*)')

a = '111 = dw11qdwq\n111 = aaaaa'
print repr(a)
a= pat.sub(rpl,a)
print repr(a)


I think this should answer your edit:

a = '111 = dw11qdwq\nsome other line\n111 = aaaaa'
b = re.split(r'(111 = )\S*', a)
d = ['qqqqqq','wwwwwww']
a = "".join(x + d.pop(0) if x == '111 = ' else x for x in b)
print a

edit: This should now handle non-matching text correctly.


Here is a solution to your modified question:

reduce(lambda result, (original, replacement): result.replace('111 = ' + original, '111 = ' + replacement), d, a)

It starts with a and runs through each replacement in d (represented by key-value tuples unpacked in the lambda statement to original and replacement) and applies it to the string.


Your question:

a = '111 = dw11qdwq\n111 = aaaaa'
d = ['qqqqqq','wwwwwww']
# i want to get '111 = qqqqqq\n111 = wwwwwww'

You don't need regexp.

out = '\n'.join(a1.split(' = ')[0] + ' = ' + d1 for a1,d1 in zip(a.splitlines(), d))

returns:

'111 = qqqqqq\n111 = wwwwwww'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜