0

I am having some issues with the following python. I am trying to match strings that are inside single quotes, but only capture the contents, that is, drop the single quotes themselves.

In [144]: tststr = "'hello'"

In [145]: res = re.search(r"'(.*)'", tststr)

In [146]: res.group()
Out[146]: "'hello'"

I would expect the output to only contain "hello" without the single quotes.

Thanks for your help!

1 Answer 1

3

You need to specify the group index number of the group which actually stores the captured characters. Without the index number, res.group() will print all the matched characters in your case, it was 'hello' .

res.group(1)

Ex:

>>> tststr = "'hello'"
>>> res = re.search(r"'(.*)'", tststr)
>>> res.group(1)
'hello'
Sign up to request clarification or add additional context in comments.

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.