java 正则怎么匹配符号加中文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 正则怎么匹配符号加中文相关的知识,希望对你有一定的参考价值。

java 正则匹配字符为纯数字方法:
定义正则表达式为:

String reg="^\\d+$"

获取要判断的字符串:

String str;//可以通过Scanner从控制台输入,也可以用字符串常量进行初始化

调用字符串的matches方法判断字符串为纯数字情况:

str.matches(reg);

如果是纯数字返回为true,否则返回为false;
参考技术A

具体说明使用可参照:正则匹配多种需求

下面是示例:

String[] special =  ",", ".", "?", "!", "", "", "[", "]", "(", ")", "-", "+", "=", "*", "|", "/", "@", "#",
"$", "%", "^", "&" ;
StringBuffer buffer = new StringBuffer();
for (String str : special) 
buffer.append("\\\\" + str);

String reg_special = buffer.toString();
String reg_chinese = ",。?!()【】¥、\\\\";
String regx = "^[\\\\u4e00-\\\\u9fa5" + reg_special + reg_chinese + "]+$";
String word = "你在哪,在做什么呢?";
if (word.matches(regx)) 
System.out.println(word);
  //能匹配上则输出
else 
System.out.println("未匹配到!");

Java提取文本文档中的所有网址(小案例介绍正则基础知识)

正则表达式基础以及Java中使用正则查找

定义: 正则表达式是一些用来匹配和处理文本的字符串

正则的基础(先大致了解下)

1. 正则表达式的作用

  1. 查找特定的信息(搜索)

  2. 替换一些文本(替换)

2. 正则基础知识

1. 元字符

  • . 匹配除换行符(\n)以外的任何单个字符
  • w 匹配字母、数字、下划线、汉字
  • s 匹配任意空白字符(包括空格、制表符、换页符等)
  • d 匹配数字,匹配单词的开始或结束
  • ^ 匹配字符串的开始
  • $ 匹配字符串的结束

例子:

#匹配abc开头的字符串  
^abd

#匹配8位数字的QQ号
^dddddddd$

#匹配以153开头的11位数字手机号  
^153dddddddd$  

2. 重复限定符

  • * 重复0次或更多次
  • + 重复1次或更多次
  • ? 重复0次或1次
  • n 重复n次
  • n, 重复n次或更多次
  • n,m 重复n到m次
#匹配8位数字QQ号    
^d8$

#匹配153开头11位手机号  
^(153)d8$

#匹配身份证号第7到14位(出生日期)    
^d7,14$  

#匹配以a开头的,0个或者多个以b结尾的字符串
^ab*$

3. 分组

#匹配字符串中包含0到多个ab开头
^(ab)*$

4. 条件或

正则用【|】表示或,当满足分支里任何一种条件时,就会匹配成功

#匹配手机号中联通的手机(联通号段130/131/132等)
^(130|131|132)d8$

5. 区间

正则提供‘[]’表示区间条件

  • [0-9] 限定0到9
  • [a-z] 限定a-z
  • [165] 限定某些数字

Java中使用正则表达式

这里会说一个例子:在一段txt文档中找出所有的网址

Java与正则

1. 这里有一段100000000(自己数,我也不知道几个0)行的文本,如图

技术图片

2. 高手写好的匹配url的正则(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]当然还有n多种方式

3. Java代码献上

正则表达式匹配使用方式

/**
 * 参数1 regex:我们的正则字符串
 * 参数2 就是一大段文本,这里用data表示
 */
private String filterSpecialStr(String regex, String data) 
    //sb存放正则匹配的结果
    StringBuffer sb = new StringBuffer();
    //编译正则字符串
    Pattern p = Pattern.compile(regex);
    //利用正则去匹配
    Matcher matcher = p.matcher(data);
    //如果找到了我们正则里要的东西
    while (matcher.find()) 
        //保存到sb中,"\r\n"表示找到一个放一行,就是换行
        sb.append(matcher.group() + "\r\n");
    
    return sb.toString();

4. 这里增加两个文件的读写

Java读取文本文件

private String readFile(String pathName) 
    //读取到的文件内容放到这个sb里
    StringBuffer sb = new StringBuffer();
    //The Java 7 try-with-resources syntax (Automatic Resource Management) is nice (这种写法是Java7的一种语法,自动管理资源,不理解自行百度)
    try (BufferedReader br = new BufferedReader(new FileReader(pathName))) 
        String line;
        while ((line = br.readLine()) != null) 
            sb.append(line + "\r\n");
        
        System.out.println("读取文件完成");
     catch (IOException e) 
        e.printStackTrace();
    
    return sb.toString();
    

Java写入文本文件

private void writeFile(String pathName, String data) 
    try 
        //文件不存在的话新建,存在覆盖
        File file = new File(pathName);
        file.createNewFile();
        //The Java 7 try-with-resources syntax (Automatic Resource Management) is nice
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) 
            bw.write(data);
            bw.flush();
            System.out.println("文件写入完成");
         catch (IOException e) 
            e.printStackTrace();
        
     catch (IOException e) 
        e.printStackTrace();
    

5. 执行结果

测试代码

把文档.txt读取到Java中,然后处理完,最后写到我指定的文件中

public static void main(String[] args) 
    //0. 准备好正则
    String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
    //1. 读取文档
    String data = readFile("F:\\test\\文档.txt");
    //2. 正则查找
    String needData = filterSpecialStr(regex, data);
    //3. 写到某个文件中
    writeFile("F:\\test\\needData.txt", needData);

结果

技术图片

以上是关于java 正则怎么匹配符号加中文的主要内容,如果未能解决你的问题,请参考以下文章

linux学习---通配符与正则表达式

4-19 Linux中的正则表达式 --- 字符匹配

linux中 正则表达式和通配符的区别

Shell 学习:正则表达式

Shell 学习:正则表达式

Shell 学习:正则表达式