简体   繁体   中英

match pattern to its counterpart from a list of patterns

In Python, I want to have a pair like this:

patterns:

abc, def
ghi, jkl
mno, xyz

The idea is: given a string, I want to search for occurrence of any of the pattern p from patterns and when I find a match, I would like to subsititute it with its counterpart.

For example:

  • this is an abcwer string
  • this is an def wer string (replaced string)

  • lot of matches together abc-ghi-mno

  • lot of matches together def-jkl-xyz (replaced string)

Uptil now, I was replacing pattern matches with empty string and here is how I was doing this:

regExps = [ re.compile(re.escape(p), re.IGNORECASE) for p in patterns ]

def cleanseName(dirName, name):
# please ignore dirName here since I have just put here a snippet of the code
    old = name
    new = ""
    for regExp in regExps:
        if regExp.search(old):
            new = regExp.sub("", old).strip()
            old = new
    if new != "":
        new = old
        print("replaced string: %s" % new)

So, how can i substitute for a pair of strings here ? What is the pythonic way of doing this ?

You can support overlapping strings by using the function-accepting version of re.sub :

import re

substitutions = {
    "abc": "def",
    "def": "ghi",
    "ghi": "jkl",
    "jkl": "mno",
    "mno": "pqr"
}

def match_to_substitution(match):
    return substitutions[match.group()]

string = "abc def ghi jkl mno"

substitute_finder = re.compile("|".join(map(re.escape, substitutions)))

substitute_finder.sub(match_to_substitution, string)
#>>> 'def ghi jkl mno pqr'
patterns = [('abc','def'),('ghi','jkl'),('mno','xyz')]

def cleanse_name(name, patterns):
    for from_,to in patterns:
        name = re.sub(re.escape(from_), to, name, flags=re.I)
    print(name)

cleanse_name("abcghimno Smith", patterns)
# defjklxyz Smith

Is that what you're looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM