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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -