标签:
1、正则表达式
public class IntegerMatch { public static void main(String[] args) { System. out.println("-1234" .matches("(-|\\+)?\\d+")); System. out.println("1234" .matches("(-|\\+)?\\d+")); System. out.println("+1234" .matches("(-|\\+)?\\d+")); System. out.println("s1234d" .matches("(-|\\+)?\\d+")); System. out.println("test" .matches("(-|\\+)?\\d+")); } }
字符 | |
B | 指定字符B |
\xhh | 表示16进制为oxhh的字符 |
\uhhhh | 16进制表示为0xhhhh的Unicode字符 |
\t | 制表符 |
\n | 换行符 |
\r | 回车 |
\f | 换页 |
\e | 转义,Escape |
字符类 | |
. | 任何字符 |
[abc] | a、b 或 c(简单类) |
[^abc] | 任何字符,除了 a、b 或 c(否定) |
[a-zA-Z] | a 到 z 或 A 到 Z,两头的字母包括在内(范围) |
[a-d[m-p]] | a 到 d 或 m 到 p:[a-dm-p](并集) |
[a-z&&[def]] | d、e 或 f(交集) |
[a-z&&[^bc]] | a 到 z,除了 b 和 c:[ad-z](减去) |
\D | 非数字: [^0-9] |
\s | 空白字符:[ \t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | 单词字符:[a-zA-Z0-9] |
\W | 非单词字符:[^\w] |
逻辑操作符 | |
XY | Y跟在X后面 |
X|Y | X或Y |
(X) | 捕获组,可以在表达式中用\i引用第i个捕获组 |
边界匹配符 | |
^ | 行的开头 |
$ | 行的结尾 |
\b | 单词边界 |
\B | 非单词边界 |
\G | 上一个匹配的结尾 |
public class Rudolph { public static void main(String [] args) { for(String pattern : new String[]{ "Rudolph", "[rR]udolph", "[rR][aeiou][a-z]ol.*" , "R.*" }) System.out.println("Rudolph" .matches(pattern)); } }
贪婪型 | 勉强型 | 占有型 | 如何匹配 |
X? | X?? | X?+ | 一个或零个X |
X* | X*? | X*+ | 零个或多个X |
X+ | X+? | X++ | 一个或多个X |
X{n} | X{n}? | X{n}+ | 恰好n次X |
X{n,} | X{n,}? | X{n,}+ | 至少n次X |
X{n,m} | X{n,m}? | X{n,m}+ | X至少n次,且不超过m次 |
import java.util.regex.*; public class TestRegularExpression { public static void main(String[] args) { String testString = "abcabcabcdefabc"; String[] patterns = { "abcabcabcdefabc", "abc+" , "(abc)+", "(abc){2,}" }; for (String pattern : patterns) { System. out.println("Regular expression: \"" + pattern + "\"" ); Pattern p = Pattern .compile(pattern); Matcher m = p.matcher(testString); while (m.find()) { System. out.println("Match \"" + m.group() + "\" at positions " + m.start() + "-" + (m.end() - 1)); } } } }
import java.util.regex.*; public class Finding { public static void main(String[] args) { Matcher m = Pattern.compile("(\\w|‘)+" ) .matcher( "Evening is full of the linnet‘s wings" ); while(m.find()) System. out.println(m.group() + " " ); System.out.println(); } }
import java.util.regex.*; public class Groups { 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 frumious Bandersnatch." ; public static void main(String[] args) { Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$") .matcher( POEM); while (m.find()) { for (int j = 0; j <= m.groupCount(); j++)//注意,这里是小于等于 System. out.println("[" + m.group(j ) + "]"); System. out.println(); } } }
import java.util.regex.*; public class ReFlags { public static void main(String[] args) { Pattern p = Pattern. compile("^java", Pattern.CASE_INSENSITIVE | Pattern. MULTILINE); Matcher m = p.matcher( "java has regex\nJava has regex\n" + "JAVA has pretty good regular expressions\n" + "Regular expressions are in Java" ); while (m.find()) System. out.println(m.group()); } }
import java.util.regex.*; import java.util.*; public class SplitDemo { 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))); System. out.println(Arrays.toString(Pattern. compile("~~").split(input))); } }
[This, unusual use, of exclamation, points] [This, unusual use, of exclamation!!points] [This!!unusual use!!of exclamation!!points]
import java.util.regex.*; public class TheReplacements { public static void main(String[] args) throws Exception { String s = "/*! Here‘s a block of text to use as input to\n" + "the regular expression matcher. Note that we‘ll\n" + "first extract the block of text by looking for\n" + "the special delimiters, then process the\n" + "extracted block. !*/"; Matcher mInput = Pattern. compile("/\\*!(.*)!\\*/", Pattern.DOTALL) .matcher(s); // 匹配/*! !*/的正则表达式 if (mInput.find()) { s = mInput.group(1); } s = s.replaceAll( " {2,}", " " ); // 把多个空格替换为一个 s = s.replaceAll( "(?m)^ +", "" ); // 把行首的空格去掉,注意,要打开多行模式 System. out.println(s); s = s.replaceFirst( "[aeiou]", "(VOWEL1)" ); } }
import java.util.regex.*; public class TheReplacements { public static void main(String[] args) throws Exception { Pattern p = Pattern. compile("cat"); Matcher m = p.matcher( "one cat two cats in the yard"); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, m.group().toUpperCase()); } m.appendTail(sb); System. out.println(sb.toString()); } }
import java.util.regex.*; public class Resetting { public static void main(String[] args) throws Exception { Matcher m = Pattern.compile("[frb][aiu][gx]").matcher( "fix the rug with bags"); while (m.find()) System. out.print(m.group() + " " ); System. out.println(); m.reset( "fix the rig with rags"); //reset方法 while (m.find()) System. out.print(m.group() + " " ); } }
import java.io.BufferedReader; import java.io.StringReader; import java.util.Scanner; public class BetterRead { public static void main(String[] args) { Scanner stdin = new Scanner(new BufferedReader(new StringReader( "Sir Robin of Camelot\n22 1.61803" ))); System. out.println("What is your name?" ); String name = stdin.nextLine(); //读取一行 System. out.println(name); System. out.println("How old are you? What is your favorite double?"); System. out.println("(input: <age> <double>)" ); int age = stdin.nextInt(); //读取整数 double favorite = stdin.nextDouble(); //读取浮点数 System. out.println(age); System. out.println(favorite); System. out.format("Hi %s.\n" , name); System. out.format("In 5 years you will be %d.\n" , age + 5); System. out.format("My favorite double is %f." , favorite / 2); } }
import java.util.*; public class ScannerDelimiter { public static void main(String[] args) { Scanner scanner = new Scanner("12, 42, 78, 99, 42"); scanner .useDelimiter("\\s*,\\s*" ); while(scanner .hasNextInt()) //读取整数 System. out.println(scanner.nextInt()); } }
import java.util.regex.*; import java.util.*; public class ThreatAnalyzer { static String threatData = "58.27.82.161@02/10/2005\n" + "204.45.234.40@02/11/2005\n" + "58.27.82.161@02/11/2005\n" + "58.27.82.161@02/12/2005\n" + "58.27.82.161@02/12/2005\n" + "[Next log section with different data format]" ; public static void main(String[] args) { Scanner scanner = new Scanner(threatData); String pattern = "(\\d+[.]\\d+[.]\\d+[.]\\d+)@" + "(\\d{2}/\\d{2}/\\d{4})" ; while(scanner.hasNext(pattern)) { scanner.next(pattern); MatchResult match = scanner.match(); String ip = match.group(1); String date = match.group(2); System. out.format("Threat on %s from %s\n" , date,ip); } } }
标签:
原文地址:http://www.cnblogs.com/timlearn/p/4300639.html