Java Support for regular Expressions (ii)

Source: Internet
Author: User

The primary use of regular expressions:

A. Finding the part of a matching regular expression in the target string

B. Verifying that the target string conforms to a regular expression, such as verifying an email address

C. Replacing a part of a regular expression with another string in the target string

The scanner class is the scan class introduced in JDK 1.5, and the constructor of the scanner class can accept a readable object, specifically file, String, InputStream, and so on.

The following is an example of a scanner class with regular expressions to find words that begin with S, S, C, T.

 Packageregextest;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classScannertest { Public Static voidMain (String [] args)throwsfilenotfoundexception {Pattern pa= Pattern.compile ("\\b[ssct]\\w+"); Matcher Ma= Pa.matcher (""); Scanner SC=NewScanner (NewFile ("Src/regextest/scannertest.java"));  while(Sc.hasnextline ()) {Ma.reset (Sc.nextline ());  while(Ma.find ()) {System.out.println (Ma.group ()); }        }    }    }

The output is:

Scanner class scannertest Static String throws Compilessctscannerscscannersrcscannertestscscsystem

The meaning of the regular expression "\\b[ssct]\\w+" is as follows, \b represents the boundary of a word, and \b is used to manipulate words using regular expressions. W stands for 0-9 or a-Z or a-Z.

Sc.hasnextline () can determine if scanner can take the next line, Sc.nextline () is a string that takes a line off.

Scanner also has Hasnextint (), Hasnextlong (), Hasnextbiginteger () and other methods for removing the next basic type.

The scanner default is to divide the input string by space, or you can customize the delimiter, as shown below.

Import Java.util.Scanner;  Public class ScannerTest2 {    publicstaticvoid  main (String [] args) {        = " 12,42, the  ",";                 New Scanner (str);        Sc.usedelimiter ("\\s*,\\s*");          while (Sc.hasnextint ()) {            System.out.println (Sc.nextint ());     }}}

The output is:

1242789942

Sc.usedelimiter ("\\s*,\\s*"), in which the \s represents a blank character (space, carriage return, line feed, etc.), * represents one or more, together is "," with any whitespace character before and after.

Scanner, in addition to being able to work with pattern and matcher, also supports regular expressions, as in the following example.

 Packageregextest;ImportJava.util.Scanner;ImportJava.util.regex.MatchResult;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classScannerTest3 { Public Static voidmain (string [] args) {string str= "[Email protected]/10/2012\n" + "[email protected]/11/2012\n" + "[email protecte d]/12/2012\n "+" [email protected]/12/2012\n]; Scanner SC=NewScanner (str); String pattern= "(\\d+\\.\\d+\\.\\d+\\.\\d+) @" + "(\\d{2}/\\d{2}/\\d{4})";  while(Sc.hasnext (pattern)) {Sc.next (pattern); Matchresult Mr=Sc.match (); String IP= Mr.group (1); String Date= Mr.group (2); System.out.format ("Threat on%s from%s\n", DATE,IP); } Pattern PA= Pattern.compile ("(\\d+\\.\\d+\\.\\d+\\.\\d+) @" + "(\\d{2}/\\d{2}/\\d{4})", Pattern.case_insensitive |pattern.multiline); Matcher Matcher=Pa.matcher (str);  while(Matcher.find ()) {String IP= Matcher.group (1); String Date= Matcher.group (2); System.out.format ("Threat on%s from%s\n", DATE,IP); }    }    }

The output is:

Threat on 02/10/2012 from 58.77.82.16102/11/2012 to 204.17.82.3102/12/2012 from 58.77.32.53 02/12/2012 from 28.45.32.16102/10/2012 to 58.77.82.16102/11/2012 from 204.17.82.31  02/12/2012 from 58.77.32.5302/12/2012 from 28.45.32.161

Java Support for regular Expressions (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.