java正则表达式之Pattern与Matcher类浅谈

163 篇文章 0 订阅

 

本文主要给大家介绍了关于java正则表达式之Pattern与Matcher类的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Pattern.split方法详解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/**

 * 测试Pattern.split方法

 */

 @Test

 public void testPatternSplit() {

 String str = "{0x40, 0x11, 0x00, 0x00}";

 // 分割符为:逗号, {,}, 空白符

 String regex = "[,\\{\\}\\s]";

 Pattern pattern = Pattern.compile(regex);

 /*

 * 1. split 方法用于使用正则表达式中的字符分割待匹配的字符串

 *

 * 注意:

 * 1. 如果分割符位于原字符串的起始位置,则分割的时候,会在起始位置上分割出一个""出来

 * 2. 如果有连续两个分隔符,则会在这两个分割符之间分割有一个""出来

 * */

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

 String[] results = pattern.split(str);

 System.out.println("length :" + results.length);

 for (int i = 0; i < results.length; i++) {

  System.out.println("element_" +i + " :" + results[i]);

 }

 System.out.println(Arrays.toString(results));

 /*

 * 2. split方法的limit参数的意思是使用正则表达式的分割字符将原字符串分为limit个组

 * **/

 System.out.println("\n----------- split limit test -----------");

 String[] resultsLimit = pattern.split(str, 2);

 for (int i = 0; i < resultsLimit.length; i++) {

  System.out.print(resultsLimit[i]);

 }

 }

结果:

----------- split test -----------
element_0 :
element_1 :0x40
element_2 :
element_3 :0x11
element_4 :
element_5 :0x00
element_6 :
element_7 :0x00
[, 0x40, , 0x11, , 0x00, , 0x00]

----------- split limit test -----------
0x40, 0x11, 0x00, 0x00}

Matcher的find/find/start/end方法详解

  • 测试Matcher的find方法:尝试在目标字符串中查找下一个匹配的字串,需在循环中迭代。
  • groupCount :返回当前查找所获得的匹配组的数量,不包括整个整个正则表达式的匹配。
    比如,表达式有两个子分组,则groupCount == 2
  • group(i):指的是用()包含的子分组,按照定义的顺序标识下标,当正则表达式中使用 |连接分组,那么有的分组匹配的字串可能为null。
  • start(group):返回此子分组匹配的子串在原字符串中的起始位置(包含)
    end(group):返回此子分组匹配的子串在原字符串中的结束位置(不包含)
    即子分组匹配的字符串在原字符串的位置为 [start(i),end(i)),左闭右开。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@Test

public void testMatcherGroupFindStartEnd() {

String str = "{0x40, 0x31, 0x20, 0x00}";

String regex = "([A-Za-z0-9]+)(,)";

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(str);

// 对于在整个原字符串中,找到的下一个匹配的字串

while (matcher.find()) {

 // 输出groupCount的数量

 System.out.println("groupCount : " + matcher.groupCount());

 // 0-输出整个匹配

 System.out.println("the substring of contains all group : " + matcher.group(0));

 System.out.println("group_0 start index : " + matcher.start(0) + " end :" + matcher.end(0));

 // 依次输出子分组的匹配结果

 // 如果子分组之间是通过 | 来连接的,则子分组的匹配结果有的为null

 for (int i = 1; i <= matcher.groupCount(); i++) {

 System.out.println("group_" + i + ":" + matcher.group(i));

 System.out.println("group_" + i + " start index : " + matcher.start(i) + " end :" + matcher.end(i));

 }

}

}

结果:

groupCount : 2
the substring of contains all group : 0x40,
group_0 start index : 1 end :6
group_1:0x40
group_1 start index : 1 end :5
group_2:,
group_2 start index : 5 end :6
groupCount : 2
the substring of contains all group : 0x31,
group_0 start index : 7 end :12
group_1:0x31
group_1 start index : 7 end :11
group_2:,
group_2 start index : 11 end :12
groupCount : 2
the substring of contains all group : 0x20,
group_0 start index : 13 end :18
group_1:0x20
group_1 start index : 13 end :17
group_2:,
group_2 start index : 17 end :18

Matcher的replace/append方法详解

测试Matcher的匹配替换以及追加的方法:

  1. matcher.replaceAll方法 :替换在原字符串中所有被正则表达式匹配的字串,并返回替换之后的结果
  2. matcher.replaceFirst方法 :替换在原字符串中第一个被正则表达式匹配的字串,并返回替换之后的结果
  3. matcher.appendReplacement方法 : 将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里(需while(matcher.find())进行配合迭代)
  4. matcher.appendTail(StringBuffer sb) 方法则将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里。

3和4的结合能够实现将原字符串中的某些字串替换指定字符,并返回完成替换之后的结果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

@Test

 public void testMatcherReplaceAppend() {

 String str = "{0x40, 0x31, 0x20, 0x00}";

 String regex = "([0-9A-Za-z]+)";

 Pattern pattern = Pattern.compile(regex);

 Matcher matcher = pattern.matcher(str);

 // replaceAll

 System.out.println("----------- replace all test ----------");

 String replacedAllStr = matcher.replaceAll("replace");

 System.out.println("replaced : " + replacedAllStr);

 //matcher.reset(str); // 重置被matcher的字符串

 matcher.reset(); // 重置matcher,以实现对原字符串重新搜索

 // replaceFirst

 System.out.println("------------ replace first test ---------");

 String replacedFirstStr = matcher.replaceFirst("replace");

 System.out.println("replaced first : " + replacedFirstStr);

 matcher.reset();

 // appendReplacement

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

 StringBuffer appendRepStr = new StringBuffer();

 while (matcher.find()) {

  matcher.appendReplacement(appendRepStr,"0xffff");

 }

 System.out.println(appendRepStr);

 // 最后调用appendTail将匹配剩余的字符串添加都StringBuffer的末尾

 // 注意这时要实现完整的功能:将所有匹配的内容替换并添加到appendRepStr中,剩余未匹配的继续添加到

 // appendRepStr中,相当于对原字符串进行全部的替换

 // 此时要保证,在遍历所有匹配的字串后调用appendTail方法

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

 matcher.appendTail(appendRepStr);

 System.out.println(appendRepStr);

 }

结果:

----------- replace all test ----------
replaced : {replace, replace, replace, replace}
------------ replace first test ---------
replaced first : {replace, 0x31, 0x20, 0x00}
------------- appendReplacement test ------------
{0xffff, 0xffff, 0xffff, 0xffff
------------ appendTail test ---------------
{0xffff, 0xffff, 0xffff, 0xffff}

测试文件源码地址

https://github.com/zhanglbjames/exercises/blob/master/src/test/java/huawei_8_16/TestT1.java

1-匹配字符类

方括号一次只能匹配括号内的一个字符

[abc]

a, b, or c (简单类)

[^abc]

除了a、b或c之外的任意 字符(求反)

[a-zA-Z]

a到z或A到Z ,包含(范围)

[a-zA-Z0-9]

匹配一次所有数字和字母的类型

[a-b-r]

匹配 a-b ,连接符 -,r
注意如果想要连接符起到范围的作用,应该满足如下格式[a-bc-de-gf-k]
即每个连接符占用的字符不能被其他连接符占用

圆括号一次匹配多个字符

  • 比如方括号的正则表达式"t[aeio]n"只匹配"tan"、"Ten"、"tin"和"ton",只能匹配单个字符,不能匹配"taeion"
  • 圆括号可以一次匹配多个字符,比如使用"t(a|e|i|o|oo)n"正则表达式,可以匹配"taen","tan","taeiooon"等
    也可以配合[]使用,如正则为"t([aei]|o|oo)n",则可以匹配 "taon","teoon",但是不能匹配"taeioon"

2-预定义字符类

. 匹配除换行符 \n 之外的任何单字符

比如:表达式就是“t.n”,它匹配“tan”、“ten”、“tin”和“ton”,还匹配“t#n”、“tpn”甚至“t n”。

  • \d 数字: [0-9]
  • \D 非数字: [^0-9]
  • \s 空格符: [ \t\n\x0B\f\r]
  • \S 非空格符: [^\s]
  • \w 单词字符: [a-zA-Z_0-9]
  • \W 非单词字符: [^\w]

记忆规则 大写表示取反,小写如下记忆

d:digit(数字)
s:space(空白字符)
w:word(单词字符), 注意包括下划线

3-表达式匹配次数

* 0次或者多次(允许0次的贪婪匹配)

例如,zo* 能匹配 "z" 以及 "zoo"。* 等价于{0,}

+ 1次或者多次(贪婪匹配)

例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。

? 0次或者1次 (非贪婪匹配,允许0次)

例如,"do(es)?" 可以匹配 "do" 或 "does" 。? 等价于 {0,1}。

{n} 恰好n次

{n,m} 从n次到m次

{n,} 至少n次

注意上述n为非负数

4-特殊字符需要转义

总结

到此这篇关于java正则表达式之Pattern与Matcher类的文章就介绍到这了,希望可以对你有所帮助。

 

转自:微点阅读   https://www.weidianyuedu.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import os from bs4 import BeautifulSoup import re 指定文件夹路径 folder_path = "C:/Users/test/Desktop/DIDItest" 正则表达式模式 pattern = r'<body>(.*?)</body>' 遍历文件夹中的所有文件 for root, dirs, files in os.walk(folder_path): for file in files: # 读取html文件 file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8-sig") as f: html_code = f.read() # 创建BeautifulSoup对象 soup = BeautifulSoup(html_code, 'html.parser') # 使用正则表达式匹配<body>标签内的数据 body_data = re.findall(pattern, html_code, re.DOTALL) # 剔除<p>和()</p> body_data = body_data[0].replace("<p>", "").replace("()</p>", "") # 使用正则表达式提取talk_id、时间、发送者ID和接收者ID matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<.*?''((中发言|发送)\s(.*?)\s)', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词 matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)', body_data) matches2 = re.findall(r'(?:中发言|发送)\s*(.*?)\s*(?:音频 :|图片 :)?(?:\[([^\]]+)\])?', body_data) # 处理匹配结果 for match in matches: talk_id = match[0] time = match[1] send_id = match[2] talk_type = match[3] content = match[4] # 提取第二个号码为接收号码 if len(matches1) >= 2: receive_id = matches1[3] # 替换字符 time = time.replace('年', '-').replace('月', '-').replace('日', '') talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊') content = content.replace('音频', '').replace('图片', '').replace('发送','').replace('中发言','') content = re.sub(r'\n', '', content) print("---导入完成-----") 使用python 创建sql数据库并将数据导入到sql文件中
07-17
抱歉,我之前的回答有误。以下是正确的代码示例来创建一个SQLite数据库并将数据导入到该数据库中: ```python import os import sqlite3 from bs4 import BeautifulSoup import re # 指定文件夹路径 folder_path = "C:/Users/test/Desktop/DIDItest" # 正则表达式模式 pattern = r'<body>(.*?)</body>' # 连接数据库 conn = sqlite3.connect('chat_data.db') cursor = conn.cursor() # 创建表格 cursor.execute('''CREATE TABLE IF NOT EXISTS chat_data (talk_id INT PRIMARY KEY, time TEXT, send_id INT, receive_id INT, talk_type TEXT, content TEXT)''') # 遍历文件夹中的所有文件 for root, dirs, files in os.walk(folder_path): for file in files: # 读取html文件 file_path = os.path.join(root, file) with open(file_path, "r", encoding="utf-8-sig") as f: html_code = f.read() # 创建BeautifulSoup对象 soup = BeautifulSoup(html_code, 'html.parser') # 使用正则表达式匹配<body>标签内的数据 body_data = re.findall(pattern, html_code, re.DOTALL) # 剔除<p>和()</p> body_data = body_data[0].replace("<p>", "").replace("()</p>", "") # 使用正则表达式提取talk_id、时间、发送者ID和接收者ID matches = re.findall(r'\[talkid:(\d+)\](\d+年\d+月\d+日 \d+:\d+:\d+).*?<span.*?>(\d+)<.*?>(.*?)<.*?''((中发言|发送)\s(.*?)\s)', body_data) # 提取唯一ID,时间,发送号码和私聊群聊关键词 matches1 = re.findall(r'<span.*?hint-success.*?>(\d+)', body_data) matches2 = re.findall(r'(?:中发言|发送)\s*(.*?)\s*(?:音频 :|图片 :)?(?:\[([^\]]+)\])?', body_data) # 处理匹配结果 for match in matches: talk_id = match[0] time = match[1] send_id = match[2] talk_type = match[3] content = match[4] # 提取第二个号码为接收号码 if len(matches1) >= 2: receive_id = matches1[3] else: receive_id = None # 替换字符 time = time.replace('年', '-').replace('月', '-').replace('日', '') talk_type = talk_type.replace('向', '私聊').replace('在群', '群聊') content = content.replace('音频', '').replace('图片', '').replace('发送','').replace('中发言','') content = re.sub(r'\n', '', content) # 插入数据到表格中 cursor.execute("INSERT INTO chat_data VALUES (?, ?, ?, ?, ?, ?)", (talk_id, time, send_id, receive_id, talk_type, content)) # 提交事务并关闭连接 conn.commit() conn.close() print("---导入完成并保存到数据库中---") ``` 请注意,这段代码将创建一个名为`chat_data.db`的SQLite数据库文件,并在其中创建一个名为`chat_data`的表格,并将数据导入到该表格中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值