Replace specific substrings within a given string using index-based replacements
Project description
Replace specific substrings within a given string using index-based replacements
Tested against Windows / Python 3.11 / Anaconda
pip install indextextreplace
index_text_replace(string: str, replacements: List[Tuple[int, int, str]]) -> str:
r"""
Replace specific substrings within a given string using index-based replacements.
Args:
string (str): The input string in which replacements will be made.
replacements (List[Tuple[int, int, str]]): A list of replacement tuples, where each tuple
consists of three elements:
- Start index (int): The starting index of the substring to be replaced (inclusive).
- End index (int): The ending index of the substring to be replaced (inclusive).
- Replacement text (str): The text to replace the specified substring with.
Returns:
str: A modified string with the specified replacements applied.
This function replaces substrings within the input `string` according to the provided
`replacements` list. Each replacement is defined by a start index (inclusive), an end index
(inclusive), and the replacement text. The replacement is applied to the specified substring
within the string. The modified string is then returned.
If the `replacements` list contains overlapping intervals, the behavior of the function is
defined by the order of the replacements within the list. The last replacement in the list
for a given index takes precedence.
Note:
- Indexing is 0-based.
- The function does not modify the original input string and returns a new modified string.
- If the start or end indices of a replacement are out of bounds, they are adjusted to
the nearest valid index within the string.
Raises:
ValueError: If the end index of a replacement is less than the start index."""
from indextextreplace import index_text_replace
t = '''Parenthese (altgriechisch παρένθεσις parénthesis, deutsch ‚Einschub')'''
r = [(1, 2, 'XXXX'), (13, 14, 'YYYYY')]
index_text_replace(t, r)
# "PXXXXenthese (aYYYYYgriechisch παρένθεσις parénthesis, deutsch ‚Einschub')"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
indextextreplace-0.10.tar.gz
(3.6 kB
view details)
Built Distribution
File details
Details for the file indextextreplace-0.10.tar.gz
.
File metadata
- Download URL: indextextreplace-0.10.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e889e6c457de2a9a4bf3ad6db109c79eeee0ae072f69e68b1c68f0c41f651efc |
|
MD5 | 15b37a140f61d41872a9f2caaceb4543 |
|
BLAKE2b-256 | 8efac84ca15989a9ab0d86e23d3faebbce78f343aced1c1627408f41d49c5adc |
File details
Details for the file indextextreplace-0.10-py3-none-any.whl
.
File metadata
- Download URL: indextextreplace-0.10-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31f681400c6af06a7c32b6cff987e7104014132994db2b8991ae7f2bf75005e6 |
|
MD5 | 2b5c2e4109d419986141a124a989c5e7 |
|
BLAKE2b-256 | cc081ef3fea5ffae20055b1fa037df29edb23fce97fca2be0592fe188dd00b58 |