Oleg Tarasenko
Oleg Tarasenko

Reputation: 9620

python: Match string using regular expression

I am learning regular expressions. Don't understand how to match the following pattern:

" myArray = ["Var1","Var2"];  "

Ideally I want to get the data in the array and to convert into python array

Upvotes: 1

Views: 195

Answers (4)

Jon Clements
Jon Clements

Reputation: 142256

Without using an re you could use builtin string methods and literal_eval which given your example returns a usable list object:

from ast import literal_eval
text = ' myArray = ["Var1","Var2"];  '
name, arr_text = (el.strip('; ') for el in text.split('='))
arr  = literal_eval(arr_text)
print name, arr

Then do what you want with name and arr...

Upvotes: 0

Omer Dagan
Omer Dagan

Reputation: 15996

If you are interested in just getting the data in the array, you can skip using regex and use eval instead. Consider this:

myArray = eval('["Var1","Var2"]')

If you must use the line you gave in the example, you can also use exec. However this command is somewhat dangerous and needs special care if used.

Upvotes: 0

ILJICH
ILJICH

Reputation: 36

Regular expression complexity differs much depending on variations of input. The easiest expressions that matches given string are:

>>> from re import search, findall
>>> s = ' myArray = ["Var1","Var2"];  '
>>> name, body = search(r'\s*(\w*)\s*=\s*\[(.*)\]', s).groups(0)
>>> contents = findall(r'"(\w*)"', body)
>>> name, contents
('myArray', ['Var1', 'Var2'])

"Converting" to python array can be done like this:

>>> globals().update({name: contents})
>>> myArray
['Var1', 'Var2']

Though it is actually a bad idea as it writes garbage in globals. Instead, try using separate dictionary, or something.

Upvotes: 0

joe
joe

Reputation: 617

Are the array items guaranteed to be surrounded by double-quotes?

This is a quick and dirty method:

re.findall('"([^,]+)"', source)

where source is your string. I didn't escape the double-quotes in the regex since you can also use single-quotes in Python. This returns a list of each item surrounded by double quotes so in your example: ['Var1', 'Var2']

Upvotes: 2

Related Questions