From 7622aed83bb2d26237532b9b757fcb2de9d65c1e Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Tue, 1 Jul 2025 13:15:11 -0400 Subject: challenge 328 task 1 regex solution by ysth --- challenge-328/ysth/blog.txt | 1 + challenge-328/ysth/perl/ch-1.pl | 14 ++++++++++++++ challenge-328/ysth/python/ch-1.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 challenge-328/ysth/blog.txt create mode 100644 challenge-328/ysth/perl/ch-1.pl create mode 100644 challenge-328/ysth/python/ch-1.py 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' + (? (? (?\?) +'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) + (? (? (?\?) + ''') + +for string in inputs: + new_string = replace_all_qs.sub(lambda m:m.lastgroup, string) + print(f'| {string} | {new_string} |') -- cgit