Skip to main content

A safe, multiple, simultaneous string substitution function

Project description


A safe, multiple, simultaneous string substitution function

Why do I want this?

You have a string you want to make substitutions on. You want to make many different substitutions at the same time and you want them done in a safe way. For example, you want to shift each word in "hey, how are you?" to the left by replacing "hey" with "how", "how" with "are, etc. This is a lightweight, pure python function with no dependencies to avoid package bloat when being used.

Install it!

You need to have git installed

  1. Clone the repo to your loacl machine. Type this in a command promp/terminal
git clone
  1. goto repo directory
cd mgsub-python
  1. install the module
python install
  1. You are all done!


Simply pass in a vector of strings to be modified, a vector of patterns to match and a vector of replacements. Then watch as they are safely, simultaneously replaced!

from mgsub import mgsub
mgsub(string, pattern=[], replacement=[], flags=0)

The pattern to match is supplied first and the replacement vector follows.

mgsub("hey, how are you?",["hey","how","are","you"],["how","are","you","hey"])

Matches and replacements can still be supplied as regex exressions. Flags from re can be set to modify how the regex works.

import re
mgsub("Dopazamine is not the same as Dopachloride and is still fake.", 
      [r'[Dd]opa(.*?mine)',"fake"], ["Meta\\1","real"], flags=re.I)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mgsub-0.1.0.tar.gz (3.2 kB view hashes)

Uploaded Source

Built Distribution

mgsub-0.1.0-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page