import java.util.regex.*;
...
Pattern.compile(Reg_Exp).matcher(Test_String).find(); // Java ( Use \\ instead of using \ )
import re
str(bool(re.search(Regex_Pattern, Test_String))).lower() # Python
■ Matching Specific String
https://www.hackerrank.com/challenges/matching-specific-string/problem?isFullScreen=true
Q.
You have a test string S. Your task is to match the string 'hackerrank'. This is case sensitive.
A.
(hackerrank)
hackerrank
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Regex_Test tester = new Regex_Test();
tester.checker("__________"); // Use '\\' instead of '\'.
}
}
class Regex_Test {
public void checker(String Regex_Pattern){
Scanner Input = new Scanner(System.in);
String Test_String = Input.nextLine();
Pattern p = Pattern.compile(Regex_Pattern);
Matcher m = p.matcher(Test_String);
int Count = 0;
while(m.find()){
Count += 1;
}
System.out.format("Number of matches : %d",Count);
}
}
Regex_Pattern = r'_________' # Do not delete 'r'.
import re
Test_String = input()
match = re.findall(Regex_Pattern, Test_String)
print("Number of matches :", len(match))
var Regex_Pattern = '_________';
//var Regex_Pattern = /_________/g; //Do not delete `/` and `/g`.
function processData(Test_String) {
//Enter your code here
var Regex = new RegExp(Regex_Pattern, "g");
var Array = Test_String.match(Regex);
//var Array = Test_String.match(Regex_Pattern);
console.log("Number of matches :", Array.length);
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
■ Matching Digits & Non-Digit Characters
https://www.hackerrank.com/challenges/matching-digits-non-digit-character/problem?isFullScreen=true
Q.
You have a test string S. Your task is to match the pattern 'zzXzzXzzzz'
Here z denotes a digit character, and X denotes a non-digit character.
A.
\d{2}\D\d{2}\D\d{4}
ASCII | [0-9] | [^0-9] |
Shorthand | \d | \D = [^\d] |
Unicode | [\p{Nd}] | [^\p{Nd}] |
Java | \p{Digit} | [^\p{Digit}] |
POSIX | [:digit:] | [^[:digit:]] |
■ Matching Word & Non-Word Character
https://www.hackerrank.com/challenges/matching-word-non-word/problem?isFullScreen=true
Q.
You have a test string S. Your task is to match the pattern 'zzzXzzzzzzzzzzXzzz'
Here z denotes any word character and X denotes any non-word character.
※ \w will match any word character.
( Word characters include alphanumeric characters (a-z, A-Z and 0-9) and underscores (_). )
※ \W matches any non-word character.
( Non-word characters include characters other than alphanumeric characters (a-z, A-Z and 0-9) and underscore (_). )
A.
\w{3}\W\w{10}\W\w{3}
[A-Za-z0-9_]{3}[^A-Za-z0-9_][A-Za-z0-9_]{10}[^A-Za-z0-9_][A-Za-z0-9_]{3}
\p{IsWord}{3}[^\p{IsWord}]\p{IsWord}{10}[^\p{IsWord}]\p{IsWord}{3}
[\p{L}\p{Nl}\p{Nd}\p{Pc}]{3}[^\p{L}\p{Nl}\p{Nd}\p{Pc}][\p{L}\p{Nl}\p{Nd}\p{Pc}]{10}[^\p{L}\p{Nl}\p{Nd}\p{Pc}][\p{L}\p{Nl}\p{Nd}\p{Pc}]{3}
ASCII | [A-Za-z0-9_] | [^A-Za-z0-9_] |
Shorthand | \w | \W = [^\w] |
Unicode | [\p{L}\p{Nl}\p{Nd}\p{Pc}] | [^\p{L}\p{Nl}\p{Nd}\p{Pc}] |
Java | \p{IsWord} | [^\p{IsWord}] |
POSIX | [:word:] | [^[:word:]] |
■ Matching Whitespace & Non-Whitespace Character
Q.
You have a test string S. Your task is to match the pattern 'zzXzzXzz'
Here, z denotes whitespace characters, and X denotes non-white space characters.
※ \s matches any whitespace character [ \r\n\t\f ].
※ \S matches any non-white space character.
A.
\S{2}\s\S{2}\s\S{2}
ASCII | [ \t\r\n\v\f] | [^ \t\r\n\v\f] |
Shorthand | \s | \S = [^\s] |
Unicode | [\p{Z}\t\r\n\v\f] | [^\p{Z}\t\r\n\v\f] |
Java | \p{Space} | [^\p{Space}] |
POSIX | [:space:] | [^[:space:]] |
■ Matching Start & End
https://www.hackerrank.com/challenges/matching-start-end/problem?isFullScreen=true
Q.
You have a test string S. Your task is to match the pattern 'Xzzzz.'
Here, Z denotes a word character, and X denotes a digit.
S must start with a digit X and end with . symbol.
S should be 6 characters long only.
A.
^\d\w{4}\.$
■ Matching Anything But a Newline
https://www.hackerrank.com/challenges/matching-anything-but-new-line/problem?isFullScreen=true
Q.
You have a test string S.
Your task is to write a regular expression that matches only and exactly strings of form:
'abc.def.ghi.jkx', where each variable a,b,c,d,e,f,g,h,i,j,k,x can be any single character except the newline.
※ The dot (.) matches anything (except for a newline).
A.
^.{3}\..{3}\..{3}\..{3}$
( 백슬래시 다음에 오는 문자가 특수문자일 경우 그 문자 자체를 의미 (ex) \. => . )
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Tester tester = new Tester();
tester.check("________");
}
}
class Tester {
public void check(String pattern){
Scanner scanner = new Scanner(System.in);
String testString = scanner.nextLine();
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(testString);
System.out.println(m.find());
//System.out.format("%s", m.matches());
}
}
regex_pattern = r"_________" # Do not delete 'r'.
import re
import sys
test_string = input()
match = re.match(regex_pattern, test_string) is not None
print(str(match).lower())
//print(str(bool(re.search(Regex_Pattern, input()))).lower())
var Regex_Pattern = /__________/g; //Do not delete '/' and 'g'. Replace __________ with your regex.
function processData(Test_String) {
var matches = Test_String.match(Regex_Pattern);
var match = matches != null && matches.length > 0;
console.log(match);
//console.log(!!Test_String.match(Regex_Pattern));
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input.trim();
});
process.stdin.on("end", function () {
processData(_input);
});
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][Regex](java) Repetitions (0) | 2023.04.09 |
---|---|
[HackerRank][Regex](java) Character Class (0) | 2023.04.09 |
[HackerRank][Regex] (0) | 2023.04.07 |
[HackerRank][SQL(Oracle)] 15 Days of Learning SQL (0) | 2023.04.05 |
[HackerRank][SQL(Oracle)] Interviews (0) | 2023.04.05 |