Java Regular Expressions parameters example
Java Regular Expressions parameters example
characters
.Any character (may or may not match line terminators)
\d
A digit: [0-9]
\D
A non-digit: [^0-9]
\s
A whitespace character: [ \t\n\x0B\f\r]
\S
A non-whitespace character: [^\s]
\w
A word character: [a-zA-Z_0-9]
\W
A non-word character: [^\w]
Match:
\d matches all digits
\s matches spaces
\w matches word characters
Alternatively, a capital letter means the opposite:
\D matches non-digits
\S matches non-spaces
\W matches non-word characters
Boundary Construct Description
^ The beginning of a line$ The end of a line
\b A word boundary
\B A non-word boundary
\A The beginning of the input
\G The end of the previous match
\Z The end of the input but for the final terminator, if any
\z The end of the input
Example:
private final String REGEX = "\\d"; // a single digit
Pattern p = Pattern.compile(REGEX);
// get a matcher object
Matcher m = p.matcher(INPUT);
int count = 0;
while(m.find()) {
count++;
System.out.println("Match number "
+ count);
System.out.println("start(): "
+ m.start());
System.out.println("end(): "
+ m.end());
}
Comments
Post a Comment