diff options
| author | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-07-01 13:15:11 -0400 |
|---|---|---|
| committer | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-07-01 13:16:35 -0400 |
| commit | 7622aed83bb2d26237532b9b757fcb2de9d65c1e (patch) | |
| tree | 030bf9f8424440dbdf8ac9fd61af75327ef7c653 | |
| parent | 7daf92e1dd4a2726fc578e87b6364ba4db3d5ad9 (diff) | |
| download | perlweeklychallenge-club-7622aed83bb2d26237532b9b757fcb2de9d65c1e.tar.gz perlweeklychallenge-club-7622aed83bb2d26237532b9b757fcb2de9d65c1e.tar.bz2 perlweeklychallenge-club-7622aed83bb2d26237532b9b757fcb2de9d65c1e.zip | |
challenge 328 task 1 regex solution by ysth
| -rw-r--r-- | challenge-328/ysth/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-328/ysth/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-328/ysth/python/ch-1.py | 14 |
3 files changed, 29 insertions, 0 deletions
diff --git a/challenge-328/ysth/blog.txt b/challenge-328/ysth/blog.txt new file mode 100644 index 0000000000..5d9399f4a0 --- /dev/null +++ b/challenge-328/ysth/blog.txt @@ -0,0 +1 @@ +https://blog.ysth.info/in-case-of-doubt-use-a-regex-the-weekly-challenge-328-task-1/ diff --git a/challenge-328/ysth/perl/ch-1.pl b/challenge-328/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..22572287ae --- /dev/null +++ b/challenge-328/ysth/perl/ch-1.pl @@ -0,0 +1,14 @@ +use 5.036; + +my @inputs = @ARGV; + +my $replace_all_qs = qr' + (?<a> (?<!a) \? (?! a | \? (?:\?{2})* (?![a?]) | (?:\?{2})* a )) + | (?<b> (?<! b | \? (?<! (?<!a) \? (?! \? (?:\?{2})* (?![a?]) | (?:\?{2})+ a ) ) ) \? (?!b)) + | (?<c>\?) +'x; + +for my $string (@inputs) { + my $new_string = $string =~ s/$replace_all_qs/@{[keys %+]}/gr; + printf "| %s | %s |\n", $string, $new_string; +} diff --git a/challenge-328/ysth/python/ch-1.py b/challenge-328/ysth/python/ch-1.py new file mode 100644 index 0000000000..69258def6f --- /dev/null +++ b/challenge-328/ysth/python/ch-1.py @@ -0,0 +1,14 @@ +import sys +import regex + +inputs = sys.argv[1:] + +replace_all_qs = regex.compile(r'''(?x) + (?<a> (?<!a) \? (?! a | \? (?:\?{2})* (?![a?]) | (?:\?{2})* a )) + | (?<b> (?<! b | \? (?<! (?<!a) \? (?! \? (?:\?{2})* (?![a?]) | (?:\?{2})+ a ) ) ) \? (?!b)) + | (?<c>\?) + ''') + +for string in inputs: + new_string = replace_all_qs.sub(lambda m:m.lastgroup, string) + print(f'| {string} | {new_string} |') |
