Matching Specific Characters – HackerRank Solution

In this post, we will solve Matching Specific Characters HackerRank Solution. This problem (Matching Specific Characters) is a part of HackerRank Regex series.

Objective

[]
The character class [ ] matches only one out of several characters placed inside the square brackets.

Task

You have a test string S.
Your task is to write a regex that will match S with following conditions:

  • S must be of length: 6
  • First character: 12 or 3
  • Second character: 12 or 0
  • Third character: xs or 0
  • Fourth character: 30 , A or a
  • Fifth character: xs or u
  • Sixth character: . or ,

Note

This is a regex only challenge. You are not required to write any code.
You have to fill the regex pattern in the blank (_________).

Solution – Matching Specific Characters – HackerRank Solution

Python

Regex_Pattern = r'^[123][120][xs0][30Aa][xsu][.,]$' # Do not delete 'r'.


import re

print(str(bool(re.search(Regex_Pattern, input()))).lower())

Note: This problem (Matching Specific Characters) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *