Python Copying part of string -
Python Copying part of string -
i have line
server:x.x.x.x # u:100 # p:100 # pre:0810 # tel:xxxxxxxxxx
and want re-create value 0810 after pre: value how can ?
you utilize re module ('re' stands regular expressions) solution assumes pre:
field have 4 numbers. if length of number varies, replace {4}
(expect 4) +
(expect 'one or more')
>>> import re >>> x = "server:x.x.x.x # u:100 # p:100 # pre:0810 # tel:xxxxxxxxxx" >>> num = re.findall(r'pre:(\d{4})', x)[0] # re.findall returns list >>> print num '0810'
you can read here:
https://docs.python.org/2/howto/regex.html
python string copy
Comments
Post a Comment