java正则表达式匹配-_java:正则表达式匹配

1、java中的正则表达式匹配

例子如下:

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.regex

Regular 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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值