I just started watching some regex tutorials on Plural Sight just to learn how to write a proper regex expression. So far so good, it's been alright and I decided to test my knowledge on Hackerrank. Although, the tutorial explains how to use PCRE (Perl Compatible Regular Expressions) and ECMAScript engines but I'm comfortable using PCRE at the moment so hence my code is running on PHP.
A brief overview of the question is:
Task
You have a test string S. Your task is to match the pattern XXxXXxXX Here, x denotes whitespace characters, and X denotes non-white space characters.
Note
This is a regex only challenge. You are not required to write code. You have to fill the regex pattern in the blank (_________).
$handle = fopen ("php://stdin","r");
$Test_String = fgets($handle);
if(preg_match($Regex_Pattern, $Test_String, $output_array)){
print ("true");
} else {
print ("false");
}
fclose($handle);
?>
I don't really know much about PHP, all I had to do was fill in the blank so I wrote 2 test cases to see how my regex was doing so far in regex 101 e.g AA AA AA, BB BB BC.
$Regex_Pattern = "/\S{2}\s{1}\S{2}\s{1}\S{2}/"; //Do not delete '/'. Replace __________ with your regex.
Final thoughts: I'm fully aware the example isn't meant to be complicated as it's meant to show how to use \S
and \s
. I attempted using [\S\s]+\S{2}$
but that wasn't greedy enough as it permits multiple spacing.
Are there more refined ways to do this without using \S
& \s
and if S
&\s
can be used differently to achieve the same results?
/(\S{2}\s){2}\S{2}/
or/\S{2}\s\S{2}\s\S{2}/
but it this point you are splitting hairs. There really isn't anything substantive here to review. \$\endgroup\$