30

Here is my regexp: f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)

I'd have to apply this on a file, line by line. The line by line is OK, simple reading from file, and a loop. But how do I apply the regexp to the lines?

Thanks for all the help, and sorry for the noob question.

7 Answers 7

37

The following expression returns a list; every entry of that list contains all matches of your regexp in the respective line.

>>> import re
>>> [re.findall(r'f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)',line) 
            for line in open('file.txt')]
Sign up to request clarification or add additional context in comments.

1 Comment

Even if your answer is very close to the one from Cédric Julien, I've voted yours because of the list comprehension and the explanation
19

You can try something like this :

import re
regex = re.compile("f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)")
with open("my_file.txt") as f:
    for line in f:
        result = regex.search(line)

1 Comment

Make some time measurement before precompile the pattern.
12
import re
with open('file.txt') as f:
    for line in f:
        match = re.search('f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)', line)

Note that Python automatically compiles and caches the regex, so a separate compile step is not required in this case.

Comments

6

Another way to do

import re
[line for line in open('file.txt') if re.match(r'f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)',line)]

Comments

3

use import re, then re.compile() with your pattern as an argument, and use the resulting object's match attribute on each line. something like this..

import re 
pat = re.compile(r'f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)')
for line in file:
  # use pat.match, pat.search .. etc

Comments

2
for line in file:
   line = re
           .match("f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)",line)
           .group(0)

Comments

1

I have used this aproach:

import re
#Define the search term:
pattern = r"f\(\s*([^,]+)\s*,\s*([^,]+)\s*\)" #pattern must be enclosed in quotes

#Create an empty list:
data = []

#then

for line in open(r'file.txt'):
    if line.strip():  #<-- To make sure the whole file is read
        word = re.findall(pattFinder1, line)
        data.append(str(word))   

1 Comment

Where did pattFinder1 come from...?

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.