Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching string from a file
#2
Quote:
if re.search("\s+\Start\s\",el) : #  < ------------- el is a line from the file ,,,
    print(f" START LN {el}")
This one prints tons of other lines I do not care about. Confused
No, there is a snytax error that would prevent the program from running. You cannot have a single backslash at the end of a string literal.

You need to protect against "\" being interpreted as the start of an escape sequence. I would use raw strings.

I don't think you fully understand what \ does in a regex pattern. Why are you using \Start in your pattern? \S is "match any non-whitespace character". \T doesn't have a special meaning in a re pattern. That's why you got an error.

Quote:I was sure by using “\s+” would filter the line I wanted but it does not.
Putting \s+ at the start of the pattern just forces you to have one whitespace character before Start Time. To ignore lines that contain your pattern as well as other text, include the start (^) and end ($) of string in your pattern. You might want to use "match" instead of "search". Match looks for the entire string to match the pattern. Search is happy if it finds your pattern anywhere in the string.

This might work:
with open("test.txt", "r") as file:
    for index, line in enumerate(file):
        result = re.match(r"\s*?(Start Time.*?[A|PM])\s*?$", line)
        if result:
            print(f"{index:3}: ({result.start()}, {result.end()}) {result.groups()[0]}")
Or you could just strip all the leading and starting whitespace and assume any line that starts with "Start Time" is a line you are looking for.
tester_V likes this post
Reply


Messages In This Thread
Matching string from a file - by tester_V - Mar-04-2024, 09:07 PM
RE: Matching string from a file - by deanhystad - Mar-04-2024, 10:14 PM
RE: Matching string from a file - by Gribouillis - Mar-04-2024, 10:45 PM
RE: Matching string from a file - by deanhystad - Mar-04-2024, 11:55 PM
RE: Matching string from a file - by tester_V - Mar-05-2024, 01:31 AM
RE: Matching string from a file - by Danishhafeez - Mar-05-2024, 05:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to replace a string with a file (HTML file) tester_V 1 904 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  matching a repeating string Skaperen 2 1,354 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Matching multiple parts in string fozz 31 6,913 Jun-13-2022, 09:38 AM
Last Post: fozz
  Matching Exact String(s) Extra 4 2,056 Jan-12-2022, 04:06 PM
Last Post: Extra
  Help with python code to search string in one file & replace with line in other file mforthman 26 12,348 Dec-19-2017, 07:11 PM
Last Post: Larz60+
  Searching a text file to find words matching a pattern Micael 3 104,266 Nov-07-2017, 08:52 PM
Last Post: Micael
  Matching Duplicate file names with different extentions wmc326 2 4,111 Aug-07-2017, 11:59 PM
Last Post: wavic
  find cell value with matching regular expression of a row in excel file hruday 4 31,135 Jul-05-2017, 01:02 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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