HackerRank - Regex - Introduction

© HackerRank

Matching Anything But a Newline

The dot (.) matches anything (except for a newline).

Task

123.456.abc.def

1
^(.{3}\.){3}.{3}$

Matching Digits & Non-Digit Characters

\d

The expression \d matches any digit [0-9].

\D

The expression \D matches any character that is not a digit.

Task

06-11-2015

1
(\d){2}(\D){1}(\d){2}(\D){1}(\d){4}

Matching Whitespace & Non-Whitespace Character

\s

\s matches any whitespace character [ \r\n\t\f ].

\S

\S matches any non-white space character.

For Example

match blank line

1
\r\n

Task

12 11 15

1
(\S){2}(\s){1}(\S){2}(\s){1}(\S){2}

Matching Word & Non-Word Character

\w

The expression \w will match any word character.
Word characters include alphanumeric characters (a-z, A-Z and 0-9) and underscores (_).

\W

\W matches any non-word character.
Non-word characters include characters other than alphanumeric characters (a-z, A-Z and 0-9) and underscore (_).

Task

www.hackerrank.com

1
(\w){3}(\W){1}(\w){10}(\W){1}(\w){3}

Matching Start & End

^

The ^ symbol matches the position at the start of a string.

$

The $ symbol matches the position at the end of a string.

Task

0qwer.

1
^(\d){1}(\w){4}(\.){1}$

Resources

Regex - Subdomains - Introduction