A tool for easily manipulating strings with patterns using the "Sandwich Method"
Project description
English description follows Japanese.
概要
- 正規表現よりも簡単な書き方で文字列のパターン的操作ができるツール
特徴
- 所定文字列で挟むことによって操作箇所を示す、正規表現よりも敢えて自由度が限定された「Sandwitch Method」に基づく
- この限定により、パターンマッチで頻出の「"」などの記号のエスケープが不要になる
- 文字列操作で頻出の最短一致がデフォルトになっている
使い方
import sndwch
# 所定の文字列で挟まれた部分を全て取得 [sndwch]
results = sndwch.get("a = {col='orange', col='green'}", "col='", "'")
print(results) # -> ["orange", "green"]
# 所定の文字列で挟まれた部分を全て置き換え [sndwch]
result = sndwch.rep(
"a = {col='orange', col='green'}", # 操作対象の文字列
"col='", "'", # 操作箇所を示すパターン (前方, 後方)
lambda s: s.upper(), # 操作関数
outer = False, # False: 引数・返値にpreとpostを含まない, True: 返値のみpreとpostを含む
)
print(result) # -> "a = {col='ORANGE', col='GREEN'}"
Overview
- A tool that allows pattern-based string manipulation with a simpler syntax than regular expressions.
Features
- Based on the deliberately restricted "Sandwich Method," where target segments are indicated by enclosing them with specified strings, offering less flexibility than regular expressions on purpose.
- This restriction eliminates the need to escape frequently occurring symbols (like
") in pattern matching. - Shortest match (non-greedy matching) is the default behavior, which is common in string manipulation tasks.
Usage
import sndwch
# Retrieve all substrings enclosed by specified strings [sndwch]
results = sndwch.get("a = {col='orange', col='green'}", "col='", "'")
print(results) # -> ["orange", "green"]
# Replace all substrings enclosed by specified strings [sndwch]
result = sndwch.rep(
"a = {col='orange', col='green'}", # Target string
"col='", "'", # Pattern indicating the target area (prefix, suffix)
lambda s: s.upper(), # Function to apply
outer = False, # False: pre and post are excluded in args & return value, True: included only in return value
)
print(result) # -> "a = {col='ORANGE', col='GREEN'}"
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
sndwch-0.2.0.tar.gz
(3.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sndwch-0.2.0.tar.gz.
File metadata
- Download URL: sndwch-0.2.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffce642ae304f0729dcd0aef8341a63dc8ee269aec5d4aae957b291f024927f3
|
|
| MD5 |
a214f25db7eb097045c6dae0d9bdc142
|
|
| BLAKE2b-256 |
b7829f7865747a3b7f2d85237a03fc4d3398cfe909c3de15d4b65adc4381ac77
|
File details
Details for the file sndwch-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sndwch-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25008876734c05eccbd13b0ca3d232a4e2a8855ef11ccde35a7ce964b1c1ec42
|
|
| MD5 |
af6019bcfb86c50c953fc1cdb358f3f3
|
|
| BLAKE2b-256 |
46decb7650bed06f827eb0776c8c9286d31ba41c3aeaa56feddad13b02a3af91
|