0

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.

2 Answers 2

2

You mean the text between ( and )?

Use capturing groups:

m = re.match("function (.*) {", line):
if m:
  print m.group(1)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I do mean that text.
0

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

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.