This is my code :
import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
match= re.findall(r':[\w]+', a)
print match
The output is : [':SHA', ':XX', ':12345']
I need to eliminate the colon and print the values in newline. How do i do it ?
mechanical_meat
170k25 gold badges238 silver badges231 bronze badges
4 Answers 4
just use this regex
match= re.findall(r':(\w+)', a)
to print on new line you can use for for example:
import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
match= re.findall(r':(\w+)', a)
for i in match:
print(i)
which produce this output:
SHA
XX
12345
answered Mar 3, 2016 at 7:05
sunny
1781 gold badge4 silver badges13 bronze badges
You can iterate match to get each value. In each value you can then replace the colon with empty string and print it out. Here is the code you can add to what you already have to accomplish this.
for value in match:
new = value.replace(":", "")
print new
answered Mar 3, 2016 at 6:27
Maytas Monsereenusorn
1111 gold badge1 silver badge8 bronze badges
1 Comment
OneCricketeer
I think the question is asking how to parse the text without the colon, not simply print it out
Try this,
import re
a = 'CUSTOMER:SHA\n SERVICE:XX\n MOBILE:12345\n'
for item in re.findall(r':[\w]+', a):
print item[1:]
Comments
How about this:
>>> re.findall(r':(\w+)',a)
['SHA', 'XX', '12345']
answered Mar 3, 2016 at 7:08
Iron Fist
11k2 gold badges21 silver badges36 bronze badges
Comments
lang-py
(\w+). The[ ]aren't necessary for your use since there is only one character the brackets