|
Special characters in regular expression |
|
Subject: Special characters in regular expression
Author: Alex_Raj
In response to: Regular expression with examples
Posted on: 08/13/2015 12:35:51 AM
The following characters are metacharacters used in constructing regular expression. They must be escaped by '\' if they are used as their own literal characters.
Metacharacters
\ -- The backslash
[] -- Matches a single character that is contained within the brackets.
[^ ] -- Matches a single character that is NOT contained within the brackets.
[ && ] -- Denotes a intersection like [a-g&&[e-z]], which matches 'e', 'f', or 'g'
[ - ] -- Denotes a range like [a-z]. Otherwise, like [abc-], it a literal character.
^ -- Matches the starting position within the string.
$ -- Matches the ending position within the string.
() -- Defines a group or scope.
| -- Logical OR
* -- Matches the preceding element zero or more times. For example, 'ab*' matches 'a', 'ab',
-- 'abb', etc. '(ab)*' matches '', 'ab', 'abab', and so on. '[ab]*' matches '', 'a', 'b',
-- 'ab', 'ba', 'aabab', and so on.
? -- Matches the preceding element zero or one time. x? = (x|E)
+ -- Matches the preceding element one and more times. x+ = xx*
{m} -- Matches the preceding element exactly m times.
{m,} -- Matches the preceding element at least m times.
{m,n} -- Matches the preceding element at least m but not more than n times.
Predefined characters
. -- Any single character, for example, '.at' matches 'bat' 'cat', or 'hat'. But it is
-- literal character within [ ], for example, '[.]at' matches '.at' only.
\d -- A digit: [0-9]
\D -- A non-digit: [^0-9]
\s -- A whitespace: [ \t\n\x0B\f\r]
\S -- A non-whitespace: [^\s]
\w -- A word character: [a-zA-Z0-9_]
\W -- A non-word character: [^\w]
>
> On 08/07/2015 12:55:27 AM Alex_Raj wrote:
A regular expression is a sequence of characters that define a pattern which is mainly used for: finding match -- for a given string, is there any match found for the specific pattern? validation/assertion -- for a given string, does the string fit the specific pattern?
Example: Finding match -- the regular expression '[hc]at' matches 'cat' and 'hat' in string "The cat wears a hat."
String regex = "[hc]at";
String input = "The cat wears a hat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
output:
Regex: [hc]at
Input: The cat wears a hat.
Found 'cat' from 4 to 7
Found 'hat' from 16 to 19
Example: Validation/assertion -- the input telephone number "415-555-1234" is good for the pattern '\d{3}-\d{3}-\d{4}'.
String regex = "\\d{3}-\\d{3}-\\d{4}";
String input = "415-555-1234";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
System.out.println("Match!");
} else {
System.out.println("Does not match!");
}
output:
Regex: \d{3}-\d{3}-\d{4}
Input: 415-555-1234
Match!
References:
|
|
|
|