Reputation: 5
This question is going to be similar, but looking for something completely different than one I asked a few days ago.
I have a string that is known, but is a portion of code, and it varies, I currently find it by using:
for num, line in enumerate(code, 1):
if re.match("function (.*) {", line):
That gets me through a good portion of what I need, as I need to know the line number that it starts at. My problem starts here. What I need is just the part where I am using the (.*) regular expression.
Upvotes: 0
Views: 46
Reputation: 56
The match object object which is returned contains all contents of groups. I would use re.search if 'function' isn't always at the beginning of a line and '.+' to match functions with at least one character.
line_to_fn = {}
for num, line in enumerate(code, 1):
match = re.search("function (.+) {", line)
if match:
matches = match.groups()
assert len(matches) == 1, repr(matches)
line_to_fn[num] = matches[0]
# line_to_fn: {1: 'something', 5: 'something_else'}here
Upvotes: 0
Reputation: 15184
You mean the text between (
and )
?
Use capturing groups:
m = re.match("function (.*) {", line):
if m:
print m.group(1)
Upvotes: 2