public class Main {
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
String自带split()方法 例:
public class Main {
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(" ");
split("\\W+");
split("n\\W+");
}
}
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!]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Matcher matcher=Pattern.compile("\\w").matcher("Evening is full of the linnest's wings");
while (matcher.find())
System.out.println(matcher.group());
int i=0;
while (matcher.find(i))
{
System.out.println(matcher.group());
i++;
}
}
}
组
例:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static public final String POEM="Twas brillig,and the slithy toves\n"+
"Did gyre and gimble in the wabe.\n"+
"All mimsy were the borogoves,\n"+
"And the mome raths outgrabe.\n\n"+
"Beware the Jabberwock, my son.\n"+
"The jaws that bite,the claws that catch.\n"+
"Beware the Jubjub bird,and shun\n"+
"The frumius Bandersnatch.";
public static void main(String[] args) {
Matcher matcher=Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$").matcher(POEM);
while (matcher.find())
{
for (int j=0;j<= matcher.groupCount();j++)
System.out.println("["+ matcher.group()+"]");
}
}
}
#### output:
[the slithy toves]
[the slithy toves]
[the slithy toves]
[the slithy toves]
[the slithy toves]
[in the wabe.]
[in the wabe.]
[in the wabe.]
[in the wabe.]
[in the wabe.]
[were the borogoves,]
[were the borogoves,]
[were the borogoves,]
[were the borogoves,]
[were the borogoves,]
[mome raths outgrabe.]
[mome raths outgrabe.]
[mome raths outgrabe.]
[mome raths outgrabe.]
[mome raths outgrabe.]
[Jabberwock, my son.]
[Jabberwock, my son.]
[Jabberwock, my son.]
[Jabberwock, my son.]
[Jabberwock, my son.]
[claws that catch.]
[claws that catch.]
[claws that catch.]
[claws that catch.]
[claws that catch.]
[Jubjub bird,and shun]
[Jubjub bird,and shun]
[Jubjub bird,and shun]
[Jubjub bird,and shun]
[Jubjub bird,and shun]
[The frumius Bandersnatch.]
[The frumius Bandersnatch.]
[The frumius Bandersnatch.]
[The frumius Bandersnatch.]
[The frumius Bandersnatch.]
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern=Pattern.compile("^java",Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
Matcher matcher=pattern.matcher("java has regex\nJava has regex\n"
+"JAVA has pretty good regular expressions\n" +
"Regular expressions are in Java");
while (matcher.find())
{
System.out.println(matcher.group());
}
}
}
output:
java
Java
JAVA
split()
例:
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input="This!!unusual use!!of exclamation!!points";
System.out.println(Arrays.toString(Pattern.compile("!!").split(input)));
System.out.println(Arrays.toString(Pattern.compile("!!").split(input,3)));
}
}
output:
[This, unusual use, of exclamation, points]
[This, unusual use, of exclamation!!points]
reset()
reset()方法,可以将现有的Matcher对象应用于一个新的字符序列 例:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Matcher matcher=Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
while (matcher.find())
{
System.out.println(matcher.group());
}
System.out.println("");
matcher.reset("fix the rig with rags");
while (matcher.find())
{
System.out.println(matcher.group());
}
}
}