[HackerRank][Regex](java) Character Class

728x90

 


 

■  Matching Specific Characters

https://www.hackerrank.com/challenges/matching-specific-characters/problem?isFullScreen=true 

Q.

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: 1, 2 or 3
*  Second character: 1, 2 or 0
*  Third character: x, s or 0
*  Fourth character: 3, 0 , A or a
*  Fifth character: x, s or u
*  Sixth character: . or ,

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

A.

^[123][120][xs0][30Aa][xsu][.,]$

( 길이가 고정되어 있으면 앞뒤에 ^(start), $(end) 붙힘 )

 


 

■  Excluding Specific Characters

https://www.hackerrank.com/challenges/excluding-specific-characters/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 6.
*  First character should not be a digit ( 1,2,3,4,5,6,7,8,9 or 0 ).
*  Second character should not be a lowercase vowel ( a,e,i,o or u ).
*  Third character should not be b, c, D or F.
*  Fourth character should not be a whitespace character ( \r, \n, \t, \f or  (space) ).
*  Fifth character should not be a uppercase vowel ( A,E,I,O or U ).
*  Sixth character should not be a . or , symbol.

※  The negated character class [^] matches any character that is not in the square brackets.

A.

^[^0-9][^aeiou][^bcDF][^ \r\n\t\f][^AEIOU][^,.]$

^\D[^aeiou][^bcDF]\S[^AEIOU][^,.]$

 


 

■  Matching Character Ranges

https://www.hackerrank.com/challenges/matching-range-of-characters/problem?isFullScreen=true 

Q.

Write a RegEx that will match a string satisfying the following conditions:

*  The string's length is >= 5.
*  The first character must be a lowercase English alphabetic character.
*  The second character must be a positive digit. Note that we consider zero to be neither positive nor negative.
*  The third character must not be a lowercase English alphabetic character.
*  The fourth character must not be an uppercase English alphabetic character.
*  The fifth character must be an uppercase English alphabetic character.

※  In the context of a regular expression (RegEx), a character class is a set of characters enclosed within square brackets that allows you to match one character in the set.
A hyphen (-) inside a character class specifies a range of characters where the left and right operands are the respective lower and upper bounds of the range. For example:
*  [a-z] is the same as [abcdefghijklmnopqrstuvwxyz].
*  [A-Z] is the same as [ABCDEFGHIJKLMNOPQRSTUVWXYZ].
*  [a-z] is the same as [0123456789].
In addition, if you use a caret (^) as the first character inside a character class, it will match anything that is not in that range. For example, [^0-9] matches any character that is not a digit in the inclusive range from 0 to 9. It's important to note that, when used outside of (immediately preceding) a character or character class, the caret matches the first character in the string against that character or set of characters.

A.

^[a-z][1-9][^a-z][^A-Z][A-Z]

 


 

 

반응형