mirror of
https://codeberg.org/privacy1st/py-regex-replace
synced 2024-12-22 02:26:06 +01:00
v0.1.0
This commit is contained in:
parent
d14fdcc75e
commit
78675f3c13
24
.idea/runConfigurations/main.xml
generated
24
.idea/runConfigurations/main.xml
generated
@ -1,24 +0,0 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
|
||||
<module name="py-regex-replace" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="$PROJECT_DIR$/venv/bin/python" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/py_regex_replace" />
|
||||
<option name="IS_MODULE_SDK" value="false" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/py_regex_replace/main.py" />
|
||||
<option name="PARAMETERS" value="-p ^Hi -r Hello -c 2" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<option name="EMULATE_TERMINAL" value="false" />
|
||||
<option name="MODULE_MODE" value="false" />
|
||||
<option name="REDIRECT_INPUT" value="true" />
|
||||
<option name="INPUT_FILE" value="$PROJECT_DIR$/test/stdin.txt" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
38
README.md
38
README.md
@ -1,6 +1,42 @@
|
||||
# py-regex-replace
|
||||
|
||||
perform regex-replacement on stdin and output result to stdout
|
||||
Perform regex-replacement on stdin and output result to stdout.
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
usage: py-regex-replace [-h] --pattern PATTERN --repl REPL [--count COUNT]
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
--pattern PATTERN, -p PATTERN
|
||||
regex search pattern
|
||||
--repl REPL, -r REPL replacement
|
||||
--count COUNT, -c COUNT
|
||||
expected count of pattern in string. -1 allows any
|
||||
count
|
||||
```
|
||||
|
||||
```
|
||||
usage: py-replace [-h] --text TEXT --repl REPL [--count COUNT]
|
||||
|
||||
options:
|
||||
-h, --help show this help message and exit
|
||||
--text TEXT, -t TEXT text to replace
|
||||
--repl REPL, -r REPL replacement
|
||||
--count COUNT, -c COUNT
|
||||
expected count of text in string. -1 allows any
|
||||
count
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
Some small example scripts can be found in [./test](./test). They can be run from inside the test directory:
|
||||
|
||||
```shell
|
||||
cd test
|
||||
./test.sh
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
|
||||
[metadata]
|
||||
name = py-regex-replace
|
||||
version = 0.0.2
|
||||
version = 0.1.0
|
||||
author = Daniel Langbein
|
||||
author_email = daniel@systemli.org
|
||||
description = perform regex-replacement on stdin and output result to stdout
|
||||
|
@ -19,8 +19,9 @@ def main():
|
||||
# Return all non-overlapping matches of pattern in string, as a list of strings or tuples.
|
||||
# The string is scanned left-to-right, and matches are returned in order.
|
||||
matches = re.findall(pattern, string, flags=re.MULTILINE)
|
||||
if len(matches) != count:
|
||||
print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.')
|
||||
if count != -1 and len(matches) != count:
|
||||
print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.',
|
||||
file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
# https://docs.python.org/3/library/re.html#re.sub
|
||||
@ -41,14 +42,15 @@ def parse_args():
|
||||
help='replacement',
|
||||
required=True, type=str)
|
||||
parser.add_argument('--count', '-c',
|
||||
help='expected count of pattern in string',
|
||||
help='expected count of pattern in string. -1 allows any count',
|
||||
default=1, type=int)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
string = sys.stdin.read()
|
||||
if len(string) == 0:
|
||||
print('[WARNING] The given string is empty.', file=sys.stderr)
|
||||
print('[WARNING] The given string is empty.',
|
||||
file=sys.stderr)
|
||||
|
||||
return args.pattern, args.repl, string, args.count
|
||||
|
||||
|
@ -8,8 +8,9 @@ def main():
|
||||
text, repl, string, count = parse_args()
|
||||
|
||||
occurrences = string.count(text)
|
||||
if occurrences != count:
|
||||
print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.')
|
||||
if count != -1 and occurrences != count:
|
||||
print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.',
|
||||
file=sys.stderr)
|
||||
exit(1)
|
||||
|
||||
string = string.replace(text, repl)
|
||||
@ -18,7 +19,7 @@ def main():
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(prog='py-regex-replace')
|
||||
parser = argparse.ArgumentParser(prog='py-replace')
|
||||
|
||||
parser.add_argument('--text', '-t',
|
||||
help='text to replace',
|
||||
@ -27,14 +28,15 @@ def parse_args():
|
||||
help='replacement',
|
||||
required=True, type=str)
|
||||
parser.add_argument('--count', '-c',
|
||||
help='expected count of text in string',
|
||||
help='expected count of text in string. -1 allows any count',
|
||||
default=1, type=int)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
string = sys.stdin.read()
|
||||
if len(string) == 0:
|
||||
print('[WARNING] The given string is empty.', file=sys.stderr)
|
||||
print('[WARNING] The given string is empty.',
|
||||
file=sys.stderr)
|
||||
|
||||
return args.text, args.repl, string, args.count
|
||||
|
||||
|
0
test/test.sh
Normal file → Executable file
0
test/test.sh
Normal file → Executable file
0
test/test_multiline_pattern.sh
Normal file → Executable file
0
test/test_multiline_pattern.sh
Normal file → Executable file
0
test/test_multiline_replacement.sh
Normal file → Executable file
0
test/test_multiline_replacement.sh
Normal file → Executable file
Loading…
Reference in New Issue
Block a user