java:Regex匹配

來源:互聯網
上載者:User

1、java中的Regex匹配

 

例子如下:

 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WanWan{
 
 public static void main(String[] args){
  
  //the standard form:
  
  //@static Pattern compile(String pattern),
  //Compiles a regular expression, creating a new Pattern instance in the process.
  Pattern p=Pattern.compile("Hello,A[a-z]*!");
  //@Matcher  matcher(CharSequence input)
  //Returns a Matcher for the Pattern and a given input.
  Matcher m=p.matcher("Hello,Android!");
  //@boolean  matches()
  //Tries to match the Pattern against the entire region (or the entire input, if no region has been set).
  boolean b1=m.matches();
  System.out.println(b1);
  
  //the more compact form:
  
  //@static boolean matches(String regex, CharSequence input)
  //Tries to match a given regular expression against a given input.
  boolean b2=Pattern.matches("Hello, A[a-z]*!", "Hello,Android!");
  System.out.println(b2);
  
  
 }
}

 

相關內容補充:

 

package

java.util.regexRegular expressions

A regular expression consists of literal text, meta characters, character sets, and operators. The latter three have a special meaning when encountered during the processing of a pattern.

  • Meta characters are a special means to describe single characters in the input text. A common example for a meta character is the dot '.', which, when used in a regular expression, matches any character.
  • Character sets are a convenient means to describe different characters that match a single character in the input. Character sets are enclosed in angular brackets '[' and ']' and use the dash '-' for forming ranges. A typical example is "[0-9a-fA-F]", which describes the set of all hexadecimal digits.
  • Operators modify or combine whole regular expressions, with the result being a regular expression again. An example for an operator is the asterisk '*', which, together with the regular expression preceding it, matches zero or more repetitions of that regular expression. The plus sign '+' is similar, but requires at least one occurrence.

Meta characters, the '[' and ']' that form a character set, and operators normally lose their special meaning when preceded by a backslash '/'. To get a backslash by itself, use a double backslash. Note that when using regular expressions in Java source code, some care has to be taken to get the backslashes right (due to yet another level of escaping being necessary for Java).

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.