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.
write() function and close the file. Define a pattern which you want to find inside the file. Now, open the file in the read form. Use the for loop, and inside that, use the re.search() method to find the pattern and if it searches the match, then print the output.
Inside a character range, \b represents the backspace character, for compatibility with Python's string literals. Matches the empty string, but only when it is not at the beginning or end of a word.
Python has a module named re to work with regular expressions. To use it, we need to import the module. The module defines several functions and constants to work with RegEx.
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')]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With