java正则表达式(二)

java正则表达式(二)

二、方括号匹配

在上一节中,我们讲到了,利用通配符可以匹配任意元素,但是,我们在实际开发中,往往会只匹配指定的元素,香港服务器,香港服务器,如何来实现呢?

在正则表达式中,[]是对指定的元素进行匹配,只有在[]里的元素才能参与匹配。

注:[]只能匹配单个字符,也就是说,正则表达式“t[aeio]n”只匹配“tan”、“Ten”、“tin”和“ton”,但“Toon”不匹配。

见代码示例:

RegExp { 2private Pattern patt; 3private Matcher matcher; 4   /** 5 * 方括号匹配: 只有方括号里面指定的字符才参与匹配。 6 * 也就是说,正则表达式“t[aeio]n”只匹配“tan”、“Ten”、“tin”和“ton”。 7 * 但“Toon”不匹配,因为在方括号之内只能匹配单个字符 regStr 匹配字符串 regex 正则表达式 squareReg(String regStr,String regex){.commonRegExp(regStr, regex);14 }15   private boolean commonRegExp(String regStr,String regex){16boolean wildcard_Res=false;17patt=Pattern.compile(regex);18matcher=patt.matcher(regStr);19wildcard_Res= matcher.find();20return wildcard_Res;21 }22 }

TestRegExp { main(String[] args) {3RegExp re=new RegExp();4boolean wildcard_Res=false;wildcard_Res=re.squareReg(“ton”, “t[aoe]n”);7      System.out.println(wildcard_Res);}

  

posted on

,香港服务器租用无神的瞳孔,我迫切想逃离这周遭被钢筋混凝土堆架的城市,

java正则表达式(二)

相关文章:

你感兴趣的文章:

标签云: