java contains 正则表达式_JAVA程序中使用正则表达式

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* @author Administrator 测试正则表达式

*

*/

public class TestRegex {

/**

* alt+shift+j

*

* @param args

*/

public static void main(String[] args) {

/**

* 使用 正则表达式的 方式与步骤 通过Pattern的实例 得到 正则表达式的匹配器 Matcher

*

*/

// 得到 Pattern实例

// Pattern pattern = Pattern.compile("(\\w{3})(\\w{3})");

// Pattern.CASE_INSENSITIVE 不区分大小写

Pattern pattern = Pattern.compile("([abcdef]{3})([abcdef]{3})",

Pattern.CASE_INSENSITIVE);

// 得到匹配器

Matcher matcher = pattern.matcher("abcDefg");

// matches 进行全局匹配 与给定的字符串进行全匹配

boolean b = matcher.matches();

System.out.println(b);

matcher.reset();

System.out.println("-------------------");

// 得到查找到的值

// 要得到值 就一定要先find

while (matcher.find()) {

// 关于得到值

// 如果 不传值 相当于 调用了group(0) 如果要得到相应的组里面的值 则传入相应的组号

System.out.println(matcher.group());

}

// matcher.find();

System.out.println(contains("\\w", "[]"));

}

/**

* 根据给定的正则表达式去判断原字符串中是有含有匹配的子字符串

*

* @param regex

* 给定的正则表达式

* @param src

* 给定的输入原

* @return 返回结果

*/

public static boolean contains(String regex, String src) {

// 创建一个模式

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(src);

return matcher.find();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值