Python Forum
Python Regular expression, small sample works but not on file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Regular expression, small sample works but not on file
#6
In the code you posted you are running that match once on every line in the text file. (And you are ignoring the success or failure of that match). You could check the success each time and then exit the loop when you find a match.

Or, if the text file isn't too big, and there's only one place it can match, then don't do it on each line one-at-a-time, just run it on the entire file.

import re
with open('testregpad.txt') as f:
    match = re.search('event\);\">(.*)<\/a.*', f.read())
    if match:
        print(match.group(1))
    else:
        print("Couldn't find a match in the file.")
Reply


Messages In This Thread
RE: Python Regular expression, small sample works but not on file - by bowlofred - Jun-09-2021, 08:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 331 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Python beginner that needs an expression added to existing script markham 1 696 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  Recursive regular expressions in Python risu252 2 1,213 Jul-25-2023, 12:59 PM
Last Post: risu252
  Regular Expression search to comment lines of code Gman2233 5 1,661 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,662 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Convert nested sample json api data into csv in python shantanu97 3 2,808 May-21-2022, 01:30 PM
Last Post: deanhystad
  Need help with my code (regular expression) shailc 5 1,920 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,165 Mar-09-2022, 10:34 PM
Last Post: snippsat
Question Opening small size browser with python selenium not work, need help greenpine 0 1,614 Feb-07-2022, 11:36 AM
Last Post: greenpine
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,324 Sep-14-2021, 08:29 AM
Last Post: hobbyist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020