Java Regular Expression: part 1 - introduction
A regular expression is a string pattern that can be used to search, find, or extract a text from a string.
Here are some of the usage of regular expressions in reality:
- checking for the validity of phone number format.
- checking for the validity of an email address format.
- searching if a string containing some numbers, or containing special characters.
- validating password strength: must contain at least one upper case letter, at least one special character, and at least one number.
And many other cases can be used regular expression.
Nowadays, there are many programming languages that support regular expressions such as Java, C#, PHP, and JavaScript.
And although regular expression engine in each language might be implemented slightly differently, most of the basic usage are the same in all supporting languages
Regular expression defines some rules that you need to know in order to use it.
Here are some common matching symbols:
. | Represents for any characters |
\d | Represents for digits from 0 to 9; it is equivalent to [0-9] |
\D | Represents non-digits; or it is equivalent to [^0-9] |
\s | Represents for whitespace characters, including \t, \n, \f, \r |
\S | Represents for non-whitespace characters; or it is equivalent to [^\s] |
\w | Represents for word characters including a-z, A-Z, 0-9, _; or it is equivalent to [a-zA-Z_0-9] |
\W | Represents for non-word characters; or it is equivalent to [^\w] |
Quantifiers
Quantifiers are used to specify the number of appearance of characters in a pattern.
* | means matching 0 or more times. |
+ | means matching 1 or more times. |
? | means matching 1 or 0 time. |
{n} | means matching exactly n times. |
{n,} | means matching at least n times. |
{n,m} | means matching at least n times, but not more than m times. |
Here are some examples:
\d*: means the matched string can have 0 or more digits.
\w*: means the matched string can have 0 or more word characters from a to z, or digits from 0 to 9
\d+: means the matched string can contain more than one digits
\w+: means the matched string can contain more than one word characters
\d?: means the matched string can contain 0 or 1 digit
\w?: means the matched string can contain 0 or 1 word
\d{5}: means the matched string must contain exactly 5 digits
\w{3}: means the matched string must contain exactly 3 word characters
\d{3,}: means the matched string must contain at least 3 digits
\w{4,}: means the matched string must contain at least 4 word characters
\d{3,5}: means the matched string must contain at least 3 digits, but not more than 5 digits
\w{1,7}: means the matched string must contain at least 1 word, but not more than 7 word characters
--
Visit learnbyproject.net for a free Regular Expression courses and other free courses