728x90
■ Matching Same Text Again & Again
https://www.hackerrank.com/challenges/matching-same-text-again-again/problem?isFullScreen=true
Q.
You have a test string S.
Your task is to write a regex that will match S with the following conditions:
* S must be of length: 20
* 1st character: lowercase letter.
* 2nd character: word character.
* 3rd character: whitespace character.
* 4th character: non word character.
* 5th character: digit.
* 6th character: non digit.
* 7th character: uppercase letter.
* 8th character: letter (either lowercase or uppercase).
* 9th character: vowel (a, e, i , o , u, A, E, I, O or U).
* 10th character: non whitespace character.
* 11th character: should be same as 1st character.
* 12th character: should be same as 2nd character.
* 13th character: should be same as 3rd character.
* 14th character: should be same as 4th character.
* 15th character: should be same as 5th character.
* 16th character: should be same as 6th character.
* 17th character: should be same as 7th character.
* 18th character: should be same as 8th character.
* 19th character: should be same as 9th character.
* 20th character: should be same as 10th character.
※ \group_number : This tool (\1 references the first capturing group) matches the same text as previously matched by the capturing group.
(ex) (\d)\1: It can match 00, 11, 22, 33, 44, 55, 66, 77, 88 or 99.
A.
^([a-z])(\w)(\s)(\W)(\d)(\D)([A-Z])([a-zA-Z])([aeiouAEIOU])(\S)\1\2\3\4\5\6\7\8\9\10$
■ Forward References
https://www.hackerrank.com/challenges/forward-references/problem?isFullScreen=true
Q.
You have a test string S.
Your task is to write a regex which will match S, with following condition(s):
* S consists of tic or tac.
* tic should not be immediate neighbour of itself.
* The first tic must occur only when tac has appeared at least twice before.
(ex)
[Valid S]
tactactic
tactactictactic
[Invalid S]
tactactictactictictac
tactictac
※ Forward reference creates a back reference to a regex that would appear later.
Forward references are only useful if they're inside a repeated group.
Then there may arise a case in which the regex engine evaluates the backreference after the group has been matched already.
( Forward reference is supported by JGsoft, .NET, Java, Perl, PCRE, PHP, Delphi and Ruby regex flavors. )
( https://www.regular-expressions.info/backref2.html#forward )
(ex)
(\2amigo|(go!))+ => go!go!amigo
A.
^(\2tic|(tac))+$
( https://regex101.com/r/Q3k6US/1 )
( 처음 푼 답 : ^tac(tac|tactic)*$ )
■ Backreferences To Failed Groups
https://www.hackerrank.com/challenges/backreferences-to-failed-groups?isFullScreen=true
Q.
You have a test string S.
Your task is to write a regex which will match S, with following condition(s):
* S consists of 8 digits.
* S may have "-" separator such that string S gets divided in 4 parts, with each part having exactly two digits. (Eg. 12-34-56-78)
(ex)
[Valid S]
12345678
12-34-56-87
[Invalid S]
1-234-56-78
12-45-7810
※ Backreference to a capturing group that match nothing is different from backreference to a capturing group that did not participate in the match at all.
(ex)
(b?)o\1 => o
Here, b? is optional and matches nothing.
Thus, (b?) is successfully matched and capture nothing.
o is matched with o and \1 successfully matches the nothing captured by the group.
(b)?o\1 => (x)
In most regex flavors (excluding JavaScript), (b)?o\1 fails to match o.
Here, (b) fails to match at all. Since, the whole group is optional the regex engine does proceed to match o.
The regex engine now arrives at \1 which references a group that did not participate in the match attempt at all.
Thus, the backreference fails to match at all.
A.
^\d{2}(\-?)\d{2}\1\d{2}\1\d{2}$
( Java 에서는 \ 대신 \\ ( '-'문자도 마찬가지. 특수문자 그대로를 의미하는거면 앞에 백슬래시 붙여야 함 )
■ Branch Reset Groups
https://www.hackerrank.com/challenges/branch-reset-groups/problem?isFullScreen=true
Q.
You have a test string S.
Your task is to write a regex which will match S, with following condition(s):
* S consists of 8 digits.
* S must have "---", "-", "." or ":" separator such that string S gets divided in 4 parts, with each part having exactly two digits.
* S string must have exactly one kind of separator.
* Separators must have integers on both sides.
(ex)
[Valid S ]
12-34-56-78
12:34:56:78
12---34---56---78
12.34.56.78
[Invalid S ]
1-234-56-78
12-45.78:10
※ (?|regex) : A branch reset group consists of alternations and capturing groups. (?|(regex1)|(regex2))
Alternatives in branch reset group share same capturing group.
( Branch reset group is supported by Perl, PHP, Delphi and R. )
( https://www.regular-expressions.info/branchreset.html )
(ex)
(?|(Haa)|(Hee)|(bye)|(k))\1 => HaaHaa, kk
A.
^\d{2}(?|(\-)|(\.)|(\:)|(\-\-\-))\d{2}\1\d{2}\1\d{2}$
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][Regex](java) Applications 1 (0) | 2023.04.09 |
---|---|
[HackerRank][Regex](java) Assertions (0) | 2023.04.09 |
[HackerRank][Regex](java) Grouping and Capturing (0) | 2023.04.09 |
[HackerRank][Regex](java) Repetitions (0) | 2023.04.09 |
[HackerRank][Regex](java) Character Class (0) | 2023.04.09 |