0
s = "[(0, '0.105*\"function\" + 0.032*\"program\" + 0.024*\"location\"')]"

This is a string I have. How do I separate strings like "function", "program", "location" using regex in python?

I expect the output in terms of list i.e.

lst = ['function', 'program', 'location']
2
  • Anything wrong with using the regex function|program|location? Commented Aug 15, 2019 at 12:05
  • Regex is the wrong tool for parsing expressions. You want a parser (though a regex-based lexer can be a useful component in that overall architecture). Commented Aug 15, 2019 at 12:22

3 Answers 3

1

Try this:

>>> re.findall(r'"([^"]*)"', s)
['function', 'program', 'location']
Sign up to request clarification or add additional context in comments.

Comments

1

You can use re module for that, but - at least for that sample data you provided - do not have to use it to get desired output, as just str methods will suffice. That is:

s = "[(0, '0.105*\"function\" + 0.032*\"program\" + 0.024*\"location\"')]"
lst = [i for i in s.split('"') if i.isalpha()]
print(lst)

Output:

['function', 'program', 'location']

This code simply split s at " and then pick strs which consist solely of alphabetic characters and have length no less than 1.

Comments

0
import re
s = "[(0, '0.105*\"function\" + 0.032*\"program\" + 0.024*\"location\"')]"
groups = re.findall("\"([a-z]+?)\"",s) # get groups of all letters which are between two `"`
print(groups) # -> ['function', 'program', 'location']

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.