0

I want to find values that match another value with a maximum of max_sub substitutions.

I do the following:

item = 'ASDDGH'
Ref = 'ASDDGY'
max_sub = 1
rx = re.compile(item+ '{s<=' + str(max_sub) + "}")
rx.search(Ref)

Which returns the following like expected:

<regex.Match object; span=(0, 6), match='ASDDGY', fuzzy_counts=(1, 0, 0)>

However, if I do:

item = 'ASDDGH'
Ref = 'ASDAGY'
max_sub = 2
rx = re.compile(item+ '{s<=' + str(max_sub) + "}")
rx.search(Ref)

I get no matches, even though there are only 2 substitutions between the strings.

What am I doing wrong?

EDIT:

Thanks, got that working.

However, I am wondering why this shows up as a match:

item = 'WARQENW'
Ref = 'WARQRFWTAPLFDYW'
max_sub = 7
rx = re.compile("(" + item+ '){s<=' + str(max_sub) + "}")
rx.search(Ref)

The length of item and Ref are not the same, so more than just substitutions are happening, but the output suggests only 2 subs are made.

Jack Arnestad
  • 1,845
  • 13
  • 26
  • Basically, the problem is that the `{...}` quantifier affects only the atom to the right of it. You applied it to `H` only, so it did not work since `ASDA` failed to match `ASDD`. If there is a grouping construct, it is applied to all patterns inside it, and that is a solution. – Wiktor Stribiżew Jun 21 '18 at 10:03
  • @WiktorStribiżew Do you think you could comment on the edit I made above? Thanks! – Jack Arnestad Jun 21 '18 at 14:28
  • I do not know what you expect. Right now, you only search for the first match using `.search()` method. The string is matched from left to right. The pattern matches exactly 7 chars (`WARQENW` is 7 char long, and they all can be different, so your pattern is actually just `.{7}` - imagine that? :)). If you use `findall`, you will have two matches. See [this Python demo](http://rextester.com/OGKR2847). – Wiktor Stribiżew Jun 21 '18 at 18:31

1 Answers1

0

Change to: rx = re.compile("(" + item+ '){s<=' + str(max_sub) + "}")

Jack Arnestad
  • 1,845
  • 13
  • 26
  • You can accept your own answer, if it works and satisfies your needs. BTW, you can use string formatting to make this whole thing prettier. – Eli Korvigo Jun 21 '18 at 09:57