0

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
asked Mar 3, 2016 at 6:19
3
  • Hey Vidhya, Have a look at this stackoverflow.com/questions/18304835/… ___ stackoverflow.com/questions/2835559/… Commented Mar 3, 2016 at 6:22
  • 1
    @RahulDambare I need to eliminate the ' : ' in the output..How does the link match my question? This is not json ... Commented Mar 3, 2016 at 6:26
  • If you understand regex, then use a group like (\w+). The [ ] aren't necessary for your use since there is only one character the brackets Commented Mar 3, 2016 at 6:30

4 Answers 4

1

just use this regex

match= re.findall(r':(\w+)', a)

see this

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
Sign up to request clarification or add additional context in comments.

2 Comments

how extra [..]?? i dont use extra [..]
:([\w]+) and :(\w+) both do same
0

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

1 Comment

I think the question is asking how to parse the text without the colon, not simply print it out
0

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:]
answered Mar 3, 2016 at 6:33

Comments

0

How about this:

>>> re.findall(r':(\w+)',a)
['SHA', 'XX', '12345']
answered Mar 3, 2016 at 7:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.