Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matching string from a file
#6
(Mar-04-2024, 09:07 PM)tester_V Wrote: Greetings!
I’d like to match strings in files, it seems simple but I’m failing to do this…
It has multiple white spaces before the word Start Time or End Time and the Time and Date of the event
String “                                                                    Start Time  2/28/2024 8:34:34 AM ”
I tried :
  
if re.search("\s+\Start\s\Time",el) : #  < ------------- el is a line from the file ,,,
    print(f" START LN {el}")
And got an error message “ bad escape \T at position 11”
Then I tried:
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
I was sure by using “\s+” would filter the line I wanted but it does not.
Would you help me with this?

Thank you.

It seems like you're encountering issues with your regular expression syntax. Here's how you can correct it:

import re

# Sample line from the file
el = "                                                                    Start Time  2/28/2024 8:34:34 AM"

# Use raw string literal to avoid escaping issues
if re.search(r"\s+Start\s+Time", el):
    print(f"START LN {el}")
In this corrected version:

I used a raw string literal (r"...") for the regular expression to avoid issues with backslashes.
I adjusted the regular expression to \s+Start\s+Time, which matches one or more whitespace characters before and after "Start Time".
i hope This should correctly filter the lines containing "Start Time" as you intended.

Best Regard
Danish Hafeez | QA Assistant
buran write Mar-05-2024, 05:55 AM:
Clickbait link removed
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,265 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