1

I have a string like :

 string="(tag, index: develop-AB123-2s), (index: develop-CD123-2s)"

I want to extract only "develop-CD123-2s", i.e. the string that comes after "(index:" and not the one with tag. How do I do in python? Thanks!

asked Oct 12, 2016 at 22:11
0

4 Answers 4

2

Warning: I'm not the best at regex

import re
s='(tag, index: develop-AB123-2s), (index: develop-CD123-2s)'
print re.findall("\\(.*?index: ([^)]+)", s)[1] # 'develop-CD123-2s'

Regex Demo

Alternative regex

re.findall("index: ([^\s)]+)", s)[1]
answered Oct 12, 2016 at 22:18
Sign up to request clarification or add additional context in comments.

2 Comments

Always useful to use r"..." style strings with regex to avoid needing to use double slashes.
I know. Just a Java habit. Also don't need the (.*?, really.
1
>>> string.split("), (")[1].split(":")[1].split(")")[0].strip()
'develop-CD123-2s'
>>> 

The first split separates each tuple, then split on : and take the second result

answered Oct 12, 2016 at 22:16

Comments

1

You could do it like this:

string = "(tag, index: develop-AB123-2s), (index: develop-CD123-2s)"
string = string.split('(index:')
string = string[1].strip(')')
print(string)

split the string on (index: and strip off the closing curly bracket

answered Oct 12, 2016 at 22:17

Comments

1

One way is using python regex - positive lookbehind assertion

import re
string = "(tag, index: develop-AB123-2s), (index: develop-CD123-2s)"
re.findall(r"(?<=\(index:) ([^)]+)", string)

This pattern only matches things that start with (index:. You can also look at negative lookbehind assertions to try and match the (tag, part.

answered Oct 12, 2016 at 23:16

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.