Hello Programmers, In this post, you will know how to solve the HackerRank Alternative Matching Solution. This problem is a part of the Regex HackerRank Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Alternative Matching Solution
Alternations, denoted by the |
character, match a single item out of several possible items separated by the vertical bar. When used inside a character class, it will match characters; when used inside a group, it will match entire expressions (i.e., everything to the left or everything to the right of the vertical bar). We must use parentheses to limit the use of alternations.
Task
Given a test string, s, write a RegEx that matches s under the following conditions:
- must start with
Mr.
,Mrs.
,Ms.
,Dr.
orEr.
. - The rest of the string must contain only one or more English alphabetic letters (upper and lowercase).
Note: This is a RegEx-only challenge. You are not required to write code; you simply need to fill in the blank.
HackerRank Alternative Matching Solutions in Cpp
HackerRank Alternative Matching Solutions in Java
public class Solution { public static void main(String[] args) { Regex_Test tester = new Regex_Test(); tester.checker("^(?:Mr|Mrs|Ms|Dr|Er)\\.[a-zA-Z]+$"); } }
HackerRank Alternative Matching Solutions in Python
Regex_Pattern = r'^(Mr\.|Mrs\.|Ms\.|Dr\.|Er\.)[a-zA-Z]+$' # Do not delete 'r'.
HackerRank Alternative Matching Solutions in JavaScript
var Regex_Pattern = /^(Mr|Mrs|Ms|Dr|Er)\.[a-zA-Z]+$/;
HackerRank Alternative Matching Solutions in PHP
$Regex_Pattern = "/^(Mr|Mrs|Ms|Dr|Er)\.[a-zA-Z]+$/"; //Do not delete '/'. Replace __________ with your regex.
Disclaimer: This problem (Alternative Matching) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.