Regular Expression for partial string matching

I’m trying to filter a list, I can get exact matches, but I’m having trouble getting partial matches. Indices 0 & 9 are true, because they are exact, but 1 & 10 have extra stuff after it, which is closer to my real-world example. You can see some of the different versions I’ve tried are still in the code block.

Regular Expression.dyn (32.2 KB)

I frequently use difflib for partial matching, and specify a % match ratio target for building the output.

import sys
import difflib
from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()
    
string_list = IN[0]
required_string = IN[1]

result =[]

for s in string_list:
    similar_ratio = similar(s,required_string)
    if similar_ratio > 0.8: # 80 % similar
        result.append(s)
    else:
        pass
OUT = result
5 Likes

If you want to Regex, you could use r"\belevation" (or “\\belevation” I think is the syntax for those nodes if not writing straight into Python).

All depends on what match you’re looking for, and what edge cases you might expect. E.g. the code in @Ewan_Opie’s post will catch “Relevation!!” which you might not want, but miss “Internal Elevation” which you probably do. However, it can pick up typos like “Elvation” which Regex wouldn’t.

3 Likes

I came back to find the library in this thread because I’ll need it in a new script, thanks for the suggestion again! Very useful