Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression question
#2
Do you only have access to the built-in re class for matching, or can you use other things like the regex module?

With only re matching, this seems difficult. The lookbehind stuff could be useful, but it has to be fixed length, so you can't have it be all matches. Can you get the length of the string before the match? If so, you could build the pattern based on that. Here's an example. I asked it to look for matches only in the first 12 characters. It correctly finds the word inside that limit, and does not find anything that is straddling or beyond the limit. It does this by forcing the match to find at least n characters afterward, where n is calculated so that the match must be in the required portion.

So there is code here, but it doesn't modify the search string at all, just calculates the pattern.

import re

source_text = "A long string that has no evidence of the word apple early in the string"

match_before = 12
targets = ["long", "apple", "string"]

for target in targets:
    print(f"Looking for {target}...", end="")
    ignore_portion = max(len(source_text) - match_before, 0)
    pattern = re.compile(fr"{target}.{{{ignore_portion},}}$")
    if re.search(pattern, source_text):
        print(" Found")
    else:
        print(" No match")
Output:
Looking for long... Found Looking for apple... No match Looking for string... No match
Reply


Messages In This Thread
regular expression question - by Skaperen - Aug-20-2021, 12:43 AM
RE: regular expression question - by bowlofred - Aug-20-2021, 03:45 AM
RE: regular expression question - by Skaperen - Aug-21-2021, 05:16 PM
RE: regular expression question - by Gribouillis - Aug-22-2021, 10:05 AM
RE: regular expression question - by Skaperen - Aug-23-2021, 06:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 346 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,700 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,680 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,944 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,187 Mar-09-2022, 10:34 PM
Last Post: snippsat
  How can I find all combinations with a regular expression? AlekseyPython 0 1,681 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,955 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,430 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,511 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Help with a regular expression t4keheart 2 2,004 Oct-13-2020, 04:05 PM
Last Post: t4keheart

Forum Jump:

User Panel Messages

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