0

I have a file.txt which contains lines as:

address1:= ("a010")
address2:= ("b005")
address3:= ("b030")
address4:= ("b008")
address5:= ("a002")
address6:= ("b004")

I need to match only the number followed by "b" as well as its line as:

2: 005
3: 030
4: 008
6: 004 

Anybody can help me to do that using python?

2
  • 2
    show us what you have tried Commented Nov 7, 2017 at 17:38
  • 1
    "Anybody can help me to do that using python?" - Yes, I am certain that many people can help you with that. However, Stack Overflow isn't a help service. It is a question-and-answer forum. Have you attempted to write a program to do this? Do you have a specific programming question to ask? When you get the chance, please take the tour. Pay specific attention to How to Ask. Commented Nov 7, 2017 at 17:42

2 Answers 2

3

Code snippet:

foo = """
address1:= ("a010")
address2:= ("b005")
address3:= ("b030")
address4:= ("b008")
address5:= ("a002")
address6:= ("b004")
"""
import re
pattern = 'address(\d+):= \("b(.*)"\)'
result = re.findall(pattern, foo)
for item in result:
    print('{index}: {entry}'.format(index=item[0], entry=item[1]))

Execution Output:

2: 005
3: 030
4: 008
6: 004
Sign up to request clarification or add additional context in comments.

Comments

1

Use this expression and capture the group 1:

(?:[b])([0-9]{3})

Here is a sample:

https://regex101.com/r/fL4csm/2

In the case of using python, i suggest you to try to add some code that you coded instead of asking for how to code it. Check this link:

https://stackoverflow.com/help/on-topic

Comments

Your Answer

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.