0

Is there a simple method to pull content between a regex? Assume I have the following sample text

 SOME TEXT [SOME MORE TEXT] value="ssss" SOME MORE TEXT

My regex is:

 compiledRegex = re.compile('\[.*\] value=("|\').*("|\')')

This will obviously return the entire [SOME MORE TEXT] value="ssss", however I only want ssss to be returned since that's what I'm looking for

I can obviously define a parser function but I feel as if python provides some simple pythonic way to do such a task

1
  • Your regular expression is faulty. It'll match value="foo' and value='bar" which you almost certainly don't want to do. You should use this instead: r'''\[.*\] value=("|')(.*?)\1'''. Note that using a triple-quoted string obviates the need to escape the "'". Also, it's a good idea to always use raw strings (e.g. r'foo' and r"bar") when dealing with regular expressions in Python. Commented Oct 23, 2011 at 5:17

2 Answers 2

2

This is what capturing groups are designed to do.

compiledRegex = re.compile('\[.*\] value=(?:"|\')(.*)(?:"|\')') 
matches = compiledRegex.match(sampleText)
capturedGroup = matches.group(1) # grab contents of first group

The ?: inside the old groups (the parentheses) means that the group is now a non-capturing group; that is, it won't be accessible as a group in the result. I converted them to keep the output simpler, but you can leave them as capturing groups if you prefer (but then you have to use matches.group(2) instead, since the first quote would be the first captured group).

Sign up to request clarification or add additional context in comments.

Comments

0

Your original regex is too greedy: r'.*\]' won't stop at the first ']' and the second '.*' won't stop at '"'. To stop at c you could use [^c] or '.*?':

regex = re.compile(r"""\[[^]]*\] value=("|')(.*?)\1""") 

Example

m = regex.search("""SOME TEXT [SOME MORE TEXT] value="ssss" SOME MORE TEXT""")
print m.group(2)

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.