spaces inside the <...> to _
e.g.
"add of <Number A> and <Number B> = " . ( <Number A> + <Number B>
to
"add of <Number_A> and <Number_B> = " . ( <Number_A> + <Number_B>
Στις 7/4/2021 1:12 μ.μ., ο/η George Bouras έγραψε:
this loosks ok , any better idea ?
my $var = '"add of <Number A> and <Number B> = " . ( <Number A> +
<Number B> )';
my $tmp = '';
say $var;
$var =~s/<([^>]+)(?{ $tmp=$^N; $tmp=~s|\W+|_|g })>/<$tmp>/g;
say $var;
George Bouras <foo@example.com> writes:
spaces inside the <...> to _
e.g.
"add of <Number A> and <Number B> = " . ( <Number A> + <Number B>
to
"add of <Number_A> and <Number_B> = " . ( <Number_A> + <Number_B>
s/(<[^<>]*) (?=[^<>]*>)/\1_/g
The (?= ... ) part is a "zero-width positive look ahead". It consumes
no characters (so to speak) but must match for the space to match.
Perl 5.30 has, experimentally, variable-length, zero-width positive look behind patterns (it's the variable-length part that is experimental), currently limited to 255 characters. That permits
s/(?<=<[^<>]{0,254}) (?=[^<>]*>)/_/g
It would be nice to have bal() which was a pattern matching primitive
which matched balanced quote-like separators (including ones where you
had different left and right quotes, eg () or []). This was a standard primitive in languages like Snobol4 and Icon, where the pattern matching wasn't regex-derived.
Of course making an ersatz version in regex isn't impossible:
https://www.andrewzammit.com/blog/regexp-matching-balanced-parenthesis-and-quotes-greedy-non-recursive/
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 65:40:39 |
Calls: | 6,654 |
Files: | 12,200 |
Messages: | 5,331,846 |