可以使用Java正则表达式的Matcher类来判断文字中是否包含shaoxing.com.cn。示例代码如下:

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

public class RegexExample {
    public static void main(String[] args) {
        String text = "This is a sample text containing shaoxing.com.cn.";
        String pattern = ".*shaoxing\\.com\\.cn.*";

        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(text);

        if (m.matches()) {
            System.out.println("The text contains shaoxing.com.cn.");
        } else {
            System.out.println("The text does not contain shaoxing.com.cn.");
        }
    }
}

解释一下代码:

  • 首先定义一个包含待检查文本的字符串text和一个正则表达式字符串pattern
  • 正则表达式".*shaoxing\\.com\\.cn.*"表示匹配任意字符0次或多次,后面紧跟着"shaoxing.com.cn",再后面跟上任意字符0次或多次。
  • 创建一个Pattern对象,使用compile()方法将正则表达式编译成模式。
  • 使用Matcher对象的matches()方法匹配待检查文本和模式。
  • 如果匹配成功,则输出"The text contains shaoxing.com.cn.";否则输出"The text does not contain shaoxing.com.cn."。

注意,在正则表达式中,"."和""都是特殊字符,需要使用"\"进行转义。因此,在上面的示例代码中,"."被转义为"."

java正则判断文字中是否包含shaoxingcomcn

原文地址: https://www.cveoy.top/t/topic/gCWt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录