I'm trying to capitalise some file names and also apply some exceptions to it. I have a series of substitutions, of which one is
s/\b(vs|etc)[.]?\b/\L$1./gir
With this I want to match any occurrence of "vs" or "Vs" or "vs." or "VS." or "etc." or "Etc" and so on. If the source string already has a full stop (eg. "Vs.") then I don't want to add one, otherwise I do want to add one. The resulting part of the string also needs to be lowercased, so that the only valid outputs are vs.
or etc.
.
It's probably just a trivial change, but I just can't find it. Can someone let me in on the thought process to assemble the regex, please?
CodePudding user response:
Remember that a word boundary meaning is context-dependent.
Here, you need to move the word boundary before optional .
as \.\b
(i.e. when there is a .
after, say, etc
) will only match if there is a letter, digit or _
right after .
.
So, you can use
s/\b(vs|etc)\b\.?/\L$1./gir
See the Perl demo online:
#!/usr/bin/perl
use feature 'say';
use strict;
use warnings;
my $str = "vs or Vs or vs. or VS. or etc avs and metch";
say $str =~ s/\b(vs|etc)\b\.?/\L$1./gir;
# => vs. or vs. or vs. or vs. or etc. avs and metch