Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression question
#1
i want to match part of a string against a regular expression. normally i would just slice the string and pass that part to re. but my code is not in control of the calls to re methods and only the whole strings are used. the only thing my code gets to control is the pattern. i have a small string of about 10 to 20 characters and i want this to be compared in only the first 80 characters of each string being matched with this pattern. i think i could do this if i needed to skip the first 80 and match the rest. but i need to match the first 80 to this smaller string i put in the pattern, and ignore (.*) everything after 80 (variable amount). there may be strings to match that are shorter than 80 in which the whole string is to be matched. how can i make a regular expression do this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#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
#3
the pattern will be matched against many strings of likely varying length. that and what i need to match could be several things together, like 2 decimal numbers in the midst of alphabetic names (if 1 number or 3 numbers then it is not a match). the unseen code will be doing all these match tries and processing the matched strings.

it would be nice if it let me pass a function to be called for each string but it wants me to pass a pattern for re. i'm thinking that i need to not use that program and just implement my own.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Skaperen Wrote:i'm thinking that i need to not use that program
Is it an open-source program? Do you have a link?
Reply
#5
(Aug-22-2021, 10:05 AM)Gribouillis Wrote: Is it an open-source program? Do you have a link?

it's a pluggable proprietary API. i do get to see the source, but not in advance. i don't get to post the source.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular Expression search to comment lines of code Gman2233 5 451 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 851 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 998 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 1,383 Mar-09-2022, 10:34 PM
Last Post: snippsat
  How can I find all combinations with a regular expression? AlekseyPython 0 1,082 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 1,758 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 1,716 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 1,791 Jan-14-2021, 11:49 AM
Last Post: Pavel_47
  Help with a regular expression t4keheart 2 1,336 Oct-13-2020, 04:05 PM
Last Post: t4keheart
  A Question about Expression IA22 2 1,076 Aug-14-2020, 04:35 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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