thnkwthprtls
thnkwthprtls

Reputation: 3487

How to set a variable to string found by regular expression in Python

I am writing a Python script that reads through a file line by line and searches for any line of the syntax var_name = value. If one is found, it sets the variables name and value to the values from that line. This is what I have so far:

import re

for i in file:
  name = ""
  value = ""
  if re.match('^[A-Za-z0-9]*[\\s]?\=[\\s]?.*$', x):
    #operations to set variables needed here...
    #then use new variables

The problem is I learned regex in a Linux environment, and it seems that Python has a bunch of different rules and functions pertaining to it. I'm pretty new to Python as is, so it's been rather confusing trying to figure this out. I've been searching around and found a reasonable amount on checking for a regex match, but nothing on pulling out values that match once said match is found. I tried something like this:

name = re.search(' (?=[\\s]?\=.*)', x)
value = re.search(' (?<=[A-Za-z0-9]*[\\s]?\=[\\s]?) ', x)

...based on what I found here, but it doesn't work, and I'm not sure what part of the operation is incorrect. If anyone has any ideas they could share, it would be greatly appreciated. Thanks!

Upvotes: 1

Views: 2952

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

Instead of using variable you can use (?P<name>...) inside regex to get symbolic group names:

Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name.

Example:

>>> import re
>>> strs = "foo = bar"
>>> m = re.search(r'(?P<name>^\w+\s*)\=(?P<value>\s*(.*?)$)', strs)
>>> m.groupdict()
{'name': 'foo ', 'value': ' bar'}

Upvotes: 1

alecxe
alecxe

Reputation: 473873

re.search() and re.match() functions return a MatchObject or None - you should first check if it returns smth, then use group(), groups(), groupdict() or whatever method is appropriate to get what is actually extracted or found:

match = re.search(pattern, string)
if match:
    variable = match.group(<number>)

Upvotes: 2

Related Questions