[HackerRank][Regex](java) Repetitions

728x90

 


 

■  Matching {x} Repetitions

https://www.hackerrank.com/challenges/matching-x-repetitions/problem?isFullScreen=true 

Q.

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

*  S must be of length equal to 45.
*  The first 40 characters should consist of letters(both lowercase and uppercase), or of even digits.
*  The last 5 characters should consist of odd digits or whitespace characters.

※  The tool {x} will match exactly x repetitions of character/character class/groups.
(ex) w{3} : It will match the character w exactly 3 times.
(ex) [xyz]{5} : It will match the string of length 5 consisting of characters {x, y, z}. For example it will match xxxxx, xxxyy and xyxyz.
(ex) \d{4} : It will match any digit exactly 4 times.

A.

^[a-zA-Z02468]{40}[ \t\r\n\v\f13579]{5}$

^[a-zA-Z02468]{40}[ \s13579]{5}$

 

ASCII [a-zA-Z0-9] [a-zA-Z]
Shorthand    
Unicode [\p{L}\p{Nl}\p{Nd}] \p{L}\p{Nl}
Java \p{Alnum} \p{Alpha}
POSIX [:alnum:] [:alpha:]

 


 

■  Matching {x, y} Repetitions

https://www.hackerrank.com/challenges/matching-x-y-repetitions/problem?isFullScreen=true 

Q.

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

*  S should begin with 1 or 2 digits.
*  After that, S should have 3 or more letters (both lowercase and uppercase).
*  Then S should end with up to 3 . symbol(s). You can end with 0 to 3 . symbol(s), inclusively.

※  The {x,y} tool will match between x and y (both inclusive) repetitions of character/character class/group.
(ex) w{3,5} : It will match the character w 3, 4 or 5 times.
(ex) [xyz]{5,} : It will match the character x, y or z 5 or more times.
(ex) \d{1, 4} : It will match any digits 1, 2, 3, or 4 times.

A.

^[0-9]{1,2}[a-zA-Z]{3,}\.{0,3}$

^\d{1,2}[a-zA-Z]{3,}\.{0,3}$

 


 

  Matching One Or More Repetitions

https://www.hackerrank.com/challenges/matching-one-or-more-repititions/problem?isFullScreen=true 

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

*  S should begin with 1 or more digits.
*  After that, S should have 1 or more uppercase letters.
*  S should end with 1 or more lowercase letters.

※  The + tool will match one or more repetitions of character/character class/group.
(ex) w+ : It will match the character w 1 or more times.
(ex) [xyz]+ : It will match the character x, y or z 1 or more times.
(ex) \d+ : It will match any digit 1 or more times.

A.

^[0-9]+[A-Z]+[a-z]+$						-- ASCII

^\d+[A-Z]+[a-z]+$							-- Shorthand

^\p{Nd}+\p{Lu}+\p{Ll}+$					-- UNICODE

^\p{Digit}+\p{Upper}+\p{Lower}+$			-- JAVA

-- ^[[:digit:]]+[[:upper:]]+[[:lower:]]+$	-- POSIX

 

ASCII [A-Z] [a-z]
Shorthand \u \l
Unicode [\p{Lu}] \p{Ll}
Java \p{Upper} \p{Lower}
POSIX [:upper:] [:lower:]

 


 

■  Matching Zero Or More Repetitions

https://www.hackerrank.com/challenges/matching-zero-or-more-repetitions/problem?isFullScreen=true 

Q.

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

*  S should begin with 2 or more digits.
*  After that, S should have 0 or more lowercase letters.
*  S should end with 0 or more uppercase letters

※  The * tool will match zero or more repetitions of character/character class/group.
(ex) w* : It will match the character w 0 or more times.
(ex) [xyz]* : It will match the character x, y or z 0 or more times.
(ex) \d* : It will match any digit 0 or more times.

A.

^[0-9]{2,}[a-z]*[A-Z]*$

^\d{2,}[a-z]*[A-Z]*$

 


 

■  Matching Ending Items

https://www.hackerrank.com/challenges/matching-ending-items/problem?isFullScreen=true 

Q.

Write a RegEx to match a test string, S, under the following conditions:

*  S should consist of only lowercase and uppercase letters (no numbers or symbols).
*  S should end in s.

※  The $ boundary matcher matches an occurrence of a character/character class/group at the end of a line.

A.

^[a-zA-Z]*s$

 


 

 

반응형