1

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!

1
  • Did you look at findall()? Commented Sep 5, 2013 at 14:05

2 Answers 2

2

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>)
Sign up to request clarification or add additional context in comments.

Comments

1

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'}

2 Comments

One followup, in order to get those values outside of m I should call m.group('name') and m.group('value'), right?
@thnkwthprtls You can use the normal dictionary syntax too: m.groupdict()['value'], but m.group('name') also works fine.

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.