In Java, \\ means "I'm inserting a regular expression backslash, so the following character has special meaning." To insert a literal backslash, we say \\\.
// strings/IntegerMatch.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public class IntegerMatch {
public static void main(String[] args) {
System.out.println("-1234".matches("-?\\d+"));
System.out.println("5678".matches("-?\\d+"));
System.out.println("+911".matches("-?\\d+"));
System.out.println("+911".matches("(-|\\+)?\\d+"));
}
}
/* Output:
true
true
false
true
*/
// strings/Splitting.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.*;
public class Splitting {
public static String knights =
"Then, when you have found the shrubbery, "
+ "you must cut down the mightiest tree in the "
+ "forest...with... a herring!";
public static void split(String regex) {
System.out.println(Arrays.toString(knights.split(regex)));
}
public static void main(String[] args) {
split(" "); // Doesn't have to contain regex chars
split("\\W+"); // Non-word characters
split("n\\W+"); // 'n' followed by non-words
}
}
/* Output:
[Then,, when, you, have, found, the, shrubbery,, you, must, cut, down, the, mightiest, tree, in, the, forest...with..., a, herring!]
[Then, when, you, have, found, the, shrubbery, you, must, cut, down, the, mightiest, tree, in, the, forest, with, a, herring]
[The, whe, you have found the shrubbery, you must cut dow, the mightiest tree i, the forest...with... a herring!]
*/
With regular expression replacement, we can either replace the first occurrence, or all of them:
// strings/Replacing.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public class Replacing {
static String s = Splitting.knights;
public static void main(String[] args) {
System.out.println(s.replaceFirst("f\\w+", "located"));
System.out.println(s.replaceAll("shrubbery|tree|herring", "banana"));
System.out.println("forest...".matches("f\\w+"));
System.out.println("forest".matches("f\\w+"));
}
}
/* My Output:
Then, when you have located the shrubbery, you must cut down the mightiest tree in the forest...with... a herring!
Then, when you have found the banana, you must cut down the mightiest banana in the forest...with... a banana!
false
true
*/
POSIX | Non-standard | Perl/Tcl | Vim | Java | ASCII | Description |
---|---|---|---|---|---|---|
[:ascii:] [29] |
\p{ASCII} |
[\x00-\x7F] |
ASCII characters | |||
[:alnum:] |
\p{Alnum} |
[A-Za-z0-9] |
Alphanumeric characters | |||
[:word:] [29] |
\w |
\w |
\w |
[A-Za-z0-9_] |
Alphanumeric characters plus "_" | |
\W |
\W |
\W |
[^A-Za-z0-9_] |
Non-word characters | ||
[:alpha:] |
\a |
\p{Alpha} |
[A-Za-z] |
Alphabetic characters | ||
[:blank:] |
\s |
\p{Blank} |
[ \t] |
Space and tab | ||
\b |
\< \> |
\b |
(?<=\W)(?=\w)|(?<=\w)(?=\W) |
Word boundaries | ||
\B |
(?<=\W)(?=\W)|(?<=\w)(?=\w) |
Non-word boundaries | ||||
[:cntrl:] |
\p{Cntrl} |
[\x00-\x1F\x7F] |
Control characters | |||
[:digit:] |
\d |
\d |
\p{Digit} or \d |
[0-9] |
Digits | |
\D |
\D |
\D |
[^0-9] |
Non-digits | ||
[:graph:] |
\p{Graph} |
[\x21-\x7E] |
Visible characters | |||
[:lower:] |
\l |
\p{Lower} |
[a-z] |
Lowercase letters | ||
[:print:] |
\p |
\p{Print} |
[\x20-\x7E] |
Visible characters and the space character | ||
[:punct:] |
\p{Punct} |
[][!"#$%&'()*+,./:;<=>?@\^_`{|}~-] |
Punctuation characters | |||
[:space:] |
\s |
\_s |
\p{Space} or \s |
[ \t\r\n\v\f] |
Whitespace characters | |
\S |
\S |
\S |
[^ \t\r\n\v\f] |
Non-whitespace characters | ||
[:upper:] |
\u |
\p{Upper} |
[A-Z] |
Uppercase letters | ||
[:xdigit:] |
\x |
\p{XDigit} |
[A-Fa-f0-9] |
Hexadecimal digits |
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/IntegerMatch.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/Splitting.java
4.https://github.com/wangbingfeng/OnJava8-Examples/blob/master/strings/Replacing.java