This commit is contained in:
Daniel Langbein 2023-03-16 17:49:02 +01:00
parent d14fdcc75e
commit 78675f3c13
8 changed files with 51 additions and 35 deletions

View File

@ -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>

View File

@ -1,6 +1,42 @@
# py-regex-replace # 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 ## Installation

View File

@ -3,7 +3,7 @@
[metadata] [metadata]
name = py-regex-replace name = py-regex-replace
version = 0.0.2 version = 0.1.0
author = Daniel Langbein author = Daniel Langbein
author_email = daniel@systemli.org author_email = daniel@systemli.org
description = perform regex-replacement on stdin and output result to stdout description = perform regex-replacement on stdin and output result to stdout

View File

@ -19,8 +19,9 @@ def main():
# Return all non-overlapping matches of pattern in string, as a list of strings or tuples. # 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. # The string is scanned left-to-right, and matches are returned in order.
matches = re.findall(pattern, string, flags=re.MULTILINE) matches = re.findall(pattern, string, flags=re.MULTILINE)
if len(matches) != count: if count != -1 and len(matches) != count:
print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.') print(f'[ERROR] Expected {count} matches of pattern in string, but got {len(matches)}.',
file=sys.stderr)
exit(1) exit(1)
# https://docs.python.org/3/library/re.html#re.sub # https://docs.python.org/3/library/re.html#re.sub
@ -41,14 +42,15 @@ def parse_args():
help='replacement', help='replacement',
required=True, type=str) required=True, type=str)
parser.add_argument('--count', '-c', 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) default=1, type=int)
args = parser.parse_args() args = parser.parse_args()
string = sys.stdin.read() string = sys.stdin.read()
if len(string) == 0: 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 return args.pattern, args.repl, string, args.count

View File

@ -8,8 +8,9 @@ def main():
text, repl, string, count = parse_args() text, repl, string, count = parse_args()
occurrences = string.count(text) occurrences = string.count(text)
if occurrences != count: if count != -1 and occurrences != count:
print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.') print(f'[ERROR] Expected {count} occurrences of text in string, but got {occurrences}.',
file=sys.stderr)
exit(1) exit(1)
string = string.replace(text, repl) string = string.replace(text, repl)
@ -18,7 +19,7 @@ def main():
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(prog='py-regex-replace') parser = argparse.ArgumentParser(prog='py-replace')
parser.add_argument('--text', '-t', parser.add_argument('--text', '-t',
help='text to replace', help='text to replace',
@ -27,14 +28,15 @@ def parse_args():
help='replacement', help='replacement',
required=True, type=str) required=True, type=str)
parser.add_argument('--count', '-c', 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) default=1, type=int)
args = parser.parse_args() args = parser.parse_args()
string = sys.stdin.read() string = sys.stdin.read()
if len(string) == 0: 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 return args.text, args.repl, string, args.count

0
test/test.sh Normal file → Executable file
View File

0
test/test_multiline_pattern.sh Normal file → Executable file
View File

0
test/test_multiline_replacement.sh Normal file → Executable file
View File