Peter’s blog ✴ Week 369 ✴ 13 April 2026
THE WEEKLY CHALLENGE
Fun with strings
You are given a given a string caption for a video. Write a script to generate tag for the given string caption in three steps as mentioned below:
Example 1 Input: $caption = 'Cooking with 5 ingredients!' Output: '#cookingWithIngredients' Example 2 Input: $caption = 'the-last-of-the-mohicans' Output: '#thelastofthemohicans' Example 3 Input: $caption = ' extra spaces here' Output: '#extraSpacesHere' Example 4 Input: $caption = 'iPhone 15 Pro Max Review' Output: '#iphoneProMaxReview' Example 5 Input: $caption = 'Ultimate 24-Hour Challenge: Living in a Smart Home controlled entirely by Artificial Intelligence and Voice Commands in the year 2026!' Output: '#ultimateHourChallengeLivingInASmartHomeControll edEntirelyByArtificialIntelligenceAndVoiceCommandsIn'
This is all achievable using simple regexes and built-in functions, but the order is important. Applying the steps in the order listed in the challenge doesn't quite achieve the desired output, but I think this does:
I have altered some of the supplied examples to test some edge cases.
In his article, Peter presents a practical and polished approach to developing an order of operations for the sanitisation of strings. By organising the procedure so that lower case, regular expression character removal, and space & character combination are completed before the creation of camelCase, the end product meets the requirements for both camel case formatting as well as length requirements while still producing clean, effective code.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-04-13 use utf8; # Week 369 - task 1 - Valid tag use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; valid_tag('Cooking with 5 ingredients!'); valid_tag('the-last-of-the-mohicans'); valid_tag(' extra spaces here '); valid_tag('!! iPhone 15 Pro Max Review !!'); valid_tag('Ultimate 24-Hour Challenge: Living in a Smart Home controlled entirely by Artificial Intelligence and Voice Commands in the year 2026!'); sub valid_tag { my ($text); # initialise $text = $_[0]; say qq[\nInput: '$text']; $text = lc($text); # lower case all $text =~ s|[^a-z\s]||g; # remove non-letters $text =~ s|^\s+||g; # no initial spaces $text =~ s| ([a-z])|uc($1)|ge; # ucase space+letter $text =~ s|\s+||g; # no spaces $text = substr('#' . $text, 0, 100); # prefix '#' # and max 100 chars say qq[Output: '$text']; }
11 lines of code
Input: 'Cooking with 5 ingredients!' Output: '#cookingWithIngredients' Input: 'the-last-of-the-mohicans' Output: '#thelastofthemohicans' Input: ' extra spaces here ' Output: '#extraSpacesHere' Input: '!! iPhone 15 Pro Max Review !!' Output: '#iphoneProMaxReview' Input: 'Ultimate 24-Hour Challenge: Living in a Smart Home controlled entirely by Artificial Intelligence and Voice Commands in the year 2026!' Output: '#ultimateHourChallengeLivingInASmartHomeControlledEntirelyByArtifi cialIntelligenceAndVoiceCommandsIn'
Any content of this website which has been created by Peter Campbell Smith is in the public domain