perl regular expression: In a text file, any string between “<h>” and “<\\h>” wi
ID: 3787514 • Letter: P
Question
perl regular expression:
In a text file, any string between “<h>” and “<h>” will be converted in all uppercase.
s/{<h>}.*?{<h>}/uc($_)/ge;
This is what I have but it not works
for example:
Your flight to <h>Seattle</h> will depart at XXXX. Please arrive the airport 2 <h>hours</h> before flight. Check with airlines for exceptions.
will convert to:
Your flight to SEATTLE will depart at XXXX. Please arrive the airport 2 HOURS before flight. Check with airlines for exceptions.
Explanation / Answer
Here is correct perl code. Please note at top you are saying <h> and bottom you are giving example with <h>. I have used top version as that was question and not example.
my $str = 'Your flight to <h>Seattle<h> will depart at XXXX. Please arrive the airport 2 <h>hours<h> before flight. Check with airlines for exceptions.';
$str =~ s/<h>(.*?)<\h>/uc($1)/ge;
print $str;
print " ";
Sample run
$ perl -w test.pl
Your flight to SEATTLE will depart at XXXX. Please arrive the airport 2 HOURS before flight. Check with airlines for exceptions.
For example version.
my $str = 'Your flight to <h>Seattle</h> will depart at XXXX. Please arrive the airport 2 <h>hours</h> before flight. Check with airlines for exceptions.';
$str =~ s#<h>(.*?)</h>#uc($1)#ge;
print $str;
print " ";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.