活动公告

系统通知
05-18 21:22
系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

掌握正则表达式技巧让你的文本搜索能力提升百倍从基础语法到高级应用全面解析文本搜索中的正则表达式实战指南

SunJu_FaceMall

3万

主题

2860

科技点

3万

积分

白金月票

碾压王

积分
32872

塔罗立华奏

<font color=白金月票" /> 发表于 2025-9-6 19:50:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

正则表达式(Regular Expression,简称regex)是一种强大的文本模式匹配工具,它使用特定的字符序列来描述和匹配字符串模式。掌握正则表达式技巧,可以让你在处理文本搜索、数据验证、信息提取等任务时事半功倍,效率提升百倍。无论是编程开发、数据分析还是日常办公,正则表达式都是一项不可或缺的技能。

本文将从正则表达式的基础语法开始,逐步深入到高级应用技巧,通过丰富的实例和代码演示,帮助读者全面掌握正则表达式的使用方法,提升文本搜索和处理能力。

一、正则表达式基础语法

1. 字符匹配

正则表达式最基本的元素是普通字符,它们直接匹配自身。例如,正则表达式"hello"会精确匹配字符串”hello”。

示例:
  1. import re
  2. text = "hello world"
  3. pattern = "hello"
  4. result = re.search(pattern, text)
  5. print(result.group())  # 输出: hello
复制代码

2. 元字符

元字符是正则表达式中具有特殊含义的字符,它们不匹配自身,而是用于表示特定的匹配规则。常见的元字符包括:

• .:匹配除换行符外的任意单个字符
• ^:匹配字符串的开始位置
• $:匹配字符串的结束位置
• *:匹配前面的元素零次或多次
• +:匹配前面的元素一次或多次
• ?:匹配前面的元素零次或一次
• \:转义字符,用于匹配元字符本身
• |:选择符,表示”或”关系
• ():分组,将括号内的表达式作为一个整体
• []:字符类,匹配方括号内的任意字符

示例:
  1. import re
  2. # . 的使用
  3. text = "hat hot hit hut"
  4. pattern = "h.t"  # 匹配h后跟任意字符,再跟t
  5. print(re.findall(pattern, text))  # 输出: ['hat', 'hot', 'hit', 'hut']
  6. # ^ 和 $ 的使用
  7. text = "start middle end"
  8. pattern_start = "^start"  # 匹配以start开头的字符串
  9. pattern_end = "end$"      # 匹配以end结尾的字符串
  10. print(re.search(pattern_start, text).group())  # 输出: start
  11. print(re.search(pattern_end, text).group())    # 输出: end
  12. # * 的使用
  13. text = "a aa aaa aaaa"
  14. pattern = "a*"  # 匹配零个或多个a
  15. print(re.findall(pattern, text))  # 输出: ['a', '', 'aa', '', 'aaa', '', 'aaaa', '']
复制代码

3. 量词

量词用于指定匹配的次数,包括:

• {n}:精确匹配n次
• {n,}:至少匹配n次
• {n,m}:匹配n到m次

示例:
  1. import re
  2. text = "a aa aaa aaaa aaaaa"
  3. # {n} 的使用
  4. pattern = "a{3}"  # 精确匹配3个a
  5. print(re.findall(pattern, text))  # 输出: ['aaa', 'aaa', 'aaa']
  6. # {n,} 的使用
  7. pattern = "a{3,}"  # 匹配3个或更多a
  8. print(re.findall(pattern, text))  # 输出: ['aaa', 'aaaa', 'aaaaa']
  9. # {n,m} 的使用
  10. pattern = "a{2,4}"  # 匹配2到4个a
  11. print(re.findall(pattern, text))  # 输出: ['aa', 'aaa', 'aaaa', 'aaaa']
复制代码

4. 字符类

字符类用于匹配一组字符中的任意一个,可以使用连字符-表示范围,使用^表示否定。

• [abc]:匹配a、b或c
• [a-z]:匹配任意小写字母
• [A-Z]:匹配任意大写字母
• [0-9]:匹配任意数字
• [^a-z]:匹配非小写字母的任意字符

预定义字符类:

• \d:匹配数字,等同于[0-9]
• \D:匹配非数字,等同于[^0-9]
• \w:匹配单词字符(字母、数字、下划线),等同于[a-zA-Z0-9_]
• \W:匹配非单词字符,等同于[^a-zA-Z0-9_]
• \s:匹配空白字符(空格、制表符、换行符等)
• \S:匹配非空白字符

示例:
  1. import re
  2. text = "Hello World 123 Python_Regex"
  3. # 字符类的使用
  4. pattern = "[aeiou]"  # 匹配任意元音字母
  5. print(re.findall(pattern, text.lower()))  # 输出: ['e', 'o', 'o', 'e', 'e']
  6. # 预定义字符类的使用
  7. pattern_digits = "\d+"  # 匹配一个或多个数字
  8. pattern_words = "\w+"   # 匹配一个或多个单词字符
  9. print(re.findall(pattern_digits, text))  # 输出: ['123']
  10. print(re.findall(pattern_words, text))   # 输出: ['Hello', 'World', '123', 'Python_Regex']
复制代码

5. 分组和捕获

使用圆括号()可以创建分组,分组有两个主要用途:

1. 将多个元素作为一个整体应用量词
2. 捕获匹配的内容以便后续使用

示例:
  1. import re
  2. text = "2023-01-15 2023-02-20 2023-03-25"
  3. # 分组的使用
  4. pattern = "(\d{4})-(\d{2})-(\d{2})"  # 匹配日期格式并分组捕获年、月、日
  5. matches = re.findall(pattern, text)
  6. for match in matches:
  7.     year, month, day = match
  8.     print(f"Year: {year}, Month: {month}, Day: {day}")
  9. # 输出:
  10. # Year: 2023, Month: 01, Day: 15
  11. # Year: 2023, Month: 02, Day: 20
  12. # Year: 2023, Month: 03, Day: 25
  13. # 非捕获分组 (?:...)
  14. pattern = "(?:\d{4})-(\d{2})-(\d{2})"  # 年份不捕获,只捕获月和日
  15. matches = re.findall(pattern, text)
  16. print(matches)  # 输出: [('01', '15'), ('02', '20'), ('03', '25')]
复制代码

二、正则表达式进阶技巧

1. 贪婪与非贪婪匹配

默认情况下,量词是贪婪的(greedy),即尽可能多地匹配字符。在量词后加上?可以使其变为非贪婪(lazy)模式,即尽可能少地匹配字符。

示例:
  1. import re
  2. text = "<div>Content 1</div><div>Content 2</div>"
  3. # 贪婪匹配
  4. pattern_greedy = "<div>.*</div>"  # 匹配从第一个<div>到最后一个</div>之间的所有内容
  5. match_greedy = re.search(pattern_greedy, text)
  6. print(match_greedy.group())  # 输出: <div>Content 1</div><div>Content 2</div>
  7. # 非贪婪匹配
  8. pattern_lazy = "<div>.*?</div>"  # 匹配从第一个<div>到第一个</div>之间的内容
  9. match_lazy = re.search(pattern_lazy, text)
  10. print(match_lazy.group())  # 输出: <div>Content 1</div>
复制代码

2. 零宽断言

零宽断言(Zero-width Assertions)用于匹配某个位置,而不是匹配实际的字符。它们不消耗字符,只进行判断。

• (?=...):正向先行断言,表示后面的字符必须匹配...
• (?!...):负向先行断言,表示后面的字符不能匹配...
• (?<=...):正向后行断言,表示前面的字符必须匹配...
• (?<!...):负向后行断言,表示前面的字符不能匹配...

示例:
  1. import re
  2. text = "apple banana orange grape"
  3. # 正向先行断言
  4. pattern = "\w+(?= fruit)"  # 匹配后面跟着" fruit"的单词
  5. print(re.findall(pattern, "apple fruit banana fruit"))  # 输出: ['apple', 'banana']
  6. # 负向先行断言
  7. pattern = "\b(?!apple\b)\w+\b"  # 匹配不是"apple"的单词
  8. print(re.findall(pattern, text))  # 输出: ['banana', 'orange', 'grape']
  9. # 正向后行断言 (Python需要使用固定长度的模式)
  10. pattern = "(?<=\$)\d+"  # 匹配前面是$符号的数字
  11. print(re.findall(pattern, "Price: $100, $200, $300"))  # 输出: ['100', '200', '300']
  12. # 负向后行断言 (Python需要使用固定长度的模式)
  13. pattern = "\b(?<!un)\w+\b"  # 匹配前面不是"un"的单词
  14. print(re.findall(pattern, "happy unhappy joy unjoy"))  # 输出: ['happy', 'joy']
复制代码

3. 回溯控制

回溯是正则表达式引擎在匹配过程中的重要机制,但过度回溯可能导致性能问题。以下是一些控制回溯的技巧:

1. 使用原子分组(?>...):防止引擎在组内回溯
2. 使用占有量词?+,*+,++,{n,m}+:一旦匹配就不会回溯
3. 避免嵌套量词:如(a+)+可能导致灾难性回溯

示例:
  1. import re
  2. # 原子分组 (Python不支持原子分组语法,但可以通过其他方式实现类似效果)
  3. text = "aaaaaaaaaaaaaaaaaaaa"
  4. pattern = "(a+)ab"  # 普通分组,会尝试不同的a+组合,直到无法匹配
  5. pattern_atomic = "(?>a+)ab"  # 原子分组,一旦匹配a+就不会回溯
  6. # 占有量词 (Python不支持占有量词语法)
  7. # 但可以通过其他方式实现类似效果,例如使用更精确的模式
  8. text = "aaaaab"
  9. pattern_possessive = "a++ab"  # 占有量词,一旦匹配a+就不会回溯
复制代码

4. 性能优化

编写高效的正则表达式需要注意以下几点:

1. 避免嵌套量词
2. 使用非捕获分组(?:...)代替捕获分组(...)
3. 使用具体字符类代替.,如[^\n]代替.
4. 使用锚点^和$限制匹配范围
5. 预编译正则表达式(在Python中使用re.compile())

示例:
  1. import re
  2. import time
  3. # 预编译正则表达式
  4. text = "This is a sample text with multiple lines.\nSecond line here.\nThird line."
  5. pattern = re.compile(r'^\w+', re.MULTILINE)  # 预编译并设置多行模式
  6. start_time = time.time()
  7. for _ in range(10000):
  8.     re.findall(r'^\w+', text, re.MULTILINE)  # 每次都重新编译
  9. print("Without pre-compilation:", time.time() - start_time)
  10. start_time = time.time()
  11. for _ in range(10000):
  12.     pattern.findall(text)  # 使用预编译的正则表达式
  13. print("With pre-compilation:", time.time() - start_time)
复制代码

三、正则表达式在不同编程语言中的应用

1. Python中的正则表达式

Python通过re模块提供正则表达式支持,主要函数包括:

• re.match():从字符串开头匹配
• re.search():在字符串中搜索匹配
• re.findall():找到所有匹配项
• re.finditer():返回所有匹配项的迭代器
• re.sub():替换匹配项
• re.split():根据匹配项分割字符串

示例:
  1. import re
  2. # re.match() 示例
  3. text = "Python is a great programming language"
  4. pattern = r"\b\w+\b"  # 匹配单词
  5. match = re.match(pattern, text)
  6. if match:
  7.     print("Match found:", match.group())  # 输出: Match found: Python
  8. # re.search() 示例
  9. text = "The price is $100"
  10. pattern = r"\$\d+"  # 匹配价格
  11. match = re.search(pattern, text)
  12. if match:
  13.     print("Price found:", match.group())  # 输出: Price found: $100
  14. # re.findall() 示例
  15. text = "Contact us at support@example.com or info@example.com"
  16. pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"  # 匹配邮箱
  17. emails = re.findall(pattern, text)
  18. print("Emails found:", emails)  # 输出: Emails found: ['support@example.com', 'info@example.com']
  19. # re.sub() 示例
  20. text = "The quick brown fox jumps over the lazy dog."
  21. pattern = r"\b\w{4}\b"  # 匹配4个字母的单词
  22. replacement = "****"
  23. new_text = re.sub(pattern, replacement, text)
  24. print("After substitution:", new_text)  # 输出: After substitution: The quick brown **** jumps over the lazy ****.
  25. # re.split() 示例
  26. text = "apple, banana; orange| grape"
  27. pattern = r"[,;|]\s*"  # 匹配分隔符
  28. fruits = re.split(pattern, text)
  29. print("Fruits:", fruits)  # 输出: Fruits: ['apple', 'banana', 'orange', 'grape']
复制代码

2. JavaScript中的正则表达式

JavaScript中正则表达式可以使用字面量/pattern/flags或构造函数new RegExp(pattern, flags)创建。主要方法包括:

• test():测试字符串是否匹配模式
• exec():执行匹配,返回匹配结果
• match():字符串方法,返回匹配结果
• search():字符串方法,返回匹配位置
• replace():字符串方法,替换匹配项
• split():字符串方法,根据匹配项分割字符串

示例:
  1. // test() 示例
  2. const pattern = /\d+/;
  3. const text = "The price is 100 dollars";
  4. console.log(pattern.test(text));  // 输出: true
  5. // exec() 示例
  6. const pattern2 = /\b\w{5}\b/g;  // 匹配5个字母的单词,全局匹配
  7. const text2 = "Hello world, regex is powerful";
  8. let match;
  9. while ((match = pattern2.exec(text2)) !== null) {
  10.     console.log("Found word:", match[0], "at position", match.index);
  11. }
  12. // 输出:
  13. // Found word: Hello at position 0
  14. // Found word: world at position 6
  15. // Found word: regex at position 13
  16. // Found word: power at position 21
  17. // match() 示例
  18. const text3 = "Contact us at support@example.com or info@example.com";
  19. const pattern3 = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
  20. const emails = text3.match(pattern3);
  21. console.log("Emails found:", emails);  // 输出: Emails found: ['support@example.com', 'info@example.com']
  22. // replace() 示例
  23. const text4 = "The quick brown fox jumps over the lazy dog.";
  24. const pattern4 = /\b\w{4}\b/g;  // 匹配4个字母的单词
  25. const newText = text4.replace(pattern4, "****");
  26. console.log("After substitution:", newText);  // 输出: After substitution: The quick brown **** jumps over the lazy ****.
  27. // split() 示例
  28. const text5 = "apple, banana; orange| grape";
  29. const pattern5 = /[,;|]\s*/;  // 匹配分隔符
  30. const fruits = text5.split(pattern5);
  31. console.log("Fruits:", fruits);  // 输出: Fruits: ['apple', 'banana', 'orange', 'grape']
复制代码

3. Java中的正则表达式

Java通过java.util.regex包提供正则表达式支持,主要类包括:

• Pattern:表示编译后的正则表达式模式
• Matcher:用于执行匹配操作的引擎
• PatternSyntaxException:表示正则表达式语法错误

示例:
  1. import java.util.regex.*;
  2. public class RegexExample {
  3.     public static void main(String[] args) {
  4.         // Pattern 和 Matcher 示例
  5.         String text = "The price is $100";
  6.         String patternStr = "\\$\\d+";  // 匹配价格
  7.         
  8.         Pattern pattern = Pattern.compile(patternStr);
  9.         Matcher matcher = pattern.matcher(text);
  10.         
  11.         if (matcher.find()) {
  12.             System.out.println("Price found: " + matcher.group());  // 输出: Price found: $100
  13.         }
  14.         
  15.         // findAll 示例
  16.         String text2 = "Contact us at support@example.com or info@example.com";
  17.         String patternStr2 = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b";
  18.         
  19.         Pattern pattern2 = Pattern.compile(patternStr2);
  20.         Matcher matcher2 = pattern2.matcher(text2);
  21.         
  22.         System.out.print("Emails found: ");
  23.         while (matcher2.find()) {
  24.             System.out.print(matcher2.group() + " ");
  25.         }
  26.         // 输出: Emails found: support@example.com info@example.com
  27.         System.out.println();
  28.         
  29.         // replace 示例
  30.         String text3 = "The quick brown fox jumps over the lazy dog.";
  31.         String patternStr3 = "\\b\\w{4}\\b";  // 匹配4个字母的单词
  32.         String replacement = "****";
  33.         
  34.         Pattern pattern3 = Pattern.compile(patternStr3);
  35.         Matcher matcher3 = pattern3.matcher(text3);
  36.         String newText = matcher3.replaceAll(replacement);
  37.         
  38.         System.out.println("After substitution: " + newText);
  39.         // 输出: After substitution: The quick brown **** jumps over the lazy ****.
  40.         
  41.         // split 示例
  42.         String text4 = "apple, banana; orange| grape";
  43.         String patternStr4 = "[,;|]\\s*";  // 匹配分隔符
  44.         
  45.         Pattern pattern4 = Pattern.compile(patternStr4);
  46.         String[] fruits = pattern4.split(text4);
  47.         
  48.         System.out.print("Fruits: ");
  49.         for (String fruit : fruits) {
  50.             System.out.print(fruit + " ");
  51.         }
  52.         // 输出: Fruits: apple banana orange grape
  53.         System.out.println();
  54.     }
  55. }
复制代码

4. 其他语言中的正则表达式

不同编程语言对正则表达式的支持略有差异,但基本概念和语法相似。以下是几种其他语言的正则表达式使用示例:

PHP:
  1. <?php
  2. // preg_match 示例
  3. $text = "The price is $100";
  4. $pattern = '/\$\d+/';
  5. if (preg_match($pattern, $text, $matches)) {
  6.     echo "Price found: " . $matches[0] . "\n";  // 输出: Price found: $100
  7. }
  8. // preg_match_all 示例
  9. $text = "Contact us at support@example.com or info@example.com";
  10. $pattern = '/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/';
  11. if (preg_match_all($pattern, $text, $matches)) {
  12.     echo "Emails found: " . implode(", ", $matches[0]) . "\n";
  13.     // 输出: Emails found: support@example.com, info@example.com
  14. }
  15. // preg_replace 示例
  16. $text = "The quick brown fox jumps over the lazy dog.";
  17. $pattern = '/\b\w{4}\b/';
  18. $replacement = '****';
  19. $newText = preg_replace($pattern, $replacement, $text);
  20. echo "After substitution: " . $newText . "\n";
  21. // 输出: After substitution: The quick brown **** jumps over the lazy ****.
  22. // preg_split 示例
  23. $text = "apple, banana; orange| grape";
  24. $pattern = '/[,;|]\s*/';
  25. $fruits = preg_split($pattern, $text);
  26. echo "Fruits: " . implode(", ", $fruits) . "\n";
  27. // 输出: Fruits: apple, banana, orange, grape
  28. ?>
复制代码

Ruby:
  1. # =~ 操作符示例
  2. text = "The price is $100"
  3. pattern = /\$\d+/
  4. if pattern =~ text
  5.   puts "Price found: #{$&}"  # 输出: Price found: $100
  6. end
  7. # scan 方法示例
  8. text = "Contact us at support@example.com or info@example.com"
  9. pattern = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/
  10. emails = text.scan(pattern)
  11. puts "Emails found: #{emails.join(', ')}"  # 输出: Emails found: support@example.com, info@example.com
  12. # gsub 方法示例
  13. text = "The quick brown fox jumps over the lazy dog."
  14. pattern = /\b\w{4}\b/
  15. replacement = '****'
  16. new_text = text.gsub(pattern, replacement)
  17. puts "After substitution: #{new_text}"
  18. # 输出: After substitution: The quick brown **** jumps over the lazy ****.
  19. # split 方法示例
  20. text = "apple, banana; orange| grape"
  21. pattern = /[,;|]\s*/
  22. fruits = text.split(pattern)
  23. puts "Fruits: #{fruits.join(', ')}"  # 输出: Fruits: apple, banana, orange, grape
复制代码

四、实战案例

1. 数据验证

正则表达式常用于验证用户输入的数据格式,如邮箱、电话号码、身份证号等。

邮箱验证:
  1. import re
  2. def validate_email(email):
  3.     pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
  4.     return bool(re.match(pattern, email))
  5. # 测试
  6. emails = [
  7.     "user@example.com",
  8.     "user.name@example.com",
  9.     "user-name@example.co.uk",
  10.     "user@sub.example.com",
  11.     "invalid.email",
  12.     "@example.com",
  13.     "user@.com",
  14.     "user@example."
  15. ]
  16. for email in emails:
  17.     print(f"{email}: {'Valid' if validate_email(email) else 'Invalid'}")
复制代码

电话号码验证(支持多种格式):
  1. import re
  2. def validate_phone(phone):
  3.     # 支持格式:(123) 456-7890, 123-456-7890, 123.456.7890, 1234567890, +1 123 456 7890
  4.     pattern = r'^(\+\d{1,3}\s?)?(\(\d{3}\)|\d{3})[\s.-]?\d{3}[\s.-]?\d{4}$'
  5.     return bool(re.match(pattern, phone))
  6. # 测试
  7. phones = [
  8.     "(123) 456-7890",
  9.     "123-456-7890",
  10.     "123.456.7890",
  11.     "1234567890",
  12.     "+1 123 456 7890",
  13.     "123-4567",
  14.     "abc-def-ghij"
  15. ]
  16. for phone in phones:
  17.     print(f"{phone}: {'Valid' if validate_phone(phone) else 'Invalid'}")
复制代码

身份证号验证(18位中国身份证):
  1. import re
  2. def validate_id_card(id_card):
  3.     # 18位身份证号:前6位地区码,中间8位出生日期,后3位顺序码,最后1位校验码
  4.     pattern = r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$'
  5.     return bool(re.match(pattern, id_card))
  6. # 测试
  7. id_cards = [
  8.     "11010519491231002X",
  9.     "440308199901010012",
  10.     "320311770706001",
  11.     "123456789012345678"
  12. ]
  13. for id_card in id_cards:
  14.     print(f"{id_card}: {'Valid' if validate_id_card(id_card) else 'Invalid'}")
复制代码

2. 文本提取

正则表达式可以高效地从文本中提取特定信息。

从HTML中提取链接:
  1. import re
  2. def extract_links(html):
  3.     # 匹配<a>标签中的href属性
  4.     pattern = r'<a\s+(?:[^>]*?\s+)?href="([^"]*)"'
  5.     return re.findall(pattern, html)
  6. # 测试
  7. html = """
  8. <html>
  9. <body>
  10. <h1>Sample Page</h1>
  11. <p>Visit our <a href="https://example.com">website</a> or
  12. <a href="https://example.com/about">about page</a>.
  13. You can also <a href="contact.html">contact us</a>.
  14. </p>
  15. </body>
  16. </html>
  17. """
  18. links = extract_links(html)
  19. for link in links:
  20.     print(link)
  21. # 输出:
  22. # https://example.com
  23. # https://example.com/about
  24. # contact.html
复制代码

从日志文件中提取IP地址和时间戳:
  1. import re
  2. def extract_log_info(log_line):
  3.     # 匹配常见的Apache/Nginx日志格式
  4.     pattern = r'^(\S+) \S+ \S+ \[([\w:/]+\s[+\-]\d{4})\] "(\S+) (\S+) (\S+)" (\d{3}) (\d+|-)'
  5.     match = re.match(pattern, log_line)
  6.     if match:
  7.         return {
  8.             'ip': match.group(1),
  9.             'timestamp': match.group(2),
  10.             'method': match.group(3),
  11.             'path': match.group(4),
  12.             'protocol': match.group(5),
  13.             'status': match.group(6),
  14.             'size': match.group(7)
  15.         }
  16.     return None
  17. # 测试
  18. log_line = '192.168.1.1 - - [25/Dec/2023:10:00:00 +0000] "GET /index.html HTTP/1.1" 200 1024'
  19. log_info = extract_log_info(log_line)
  20. if log_info:
  21.     for key, value in log_info.items():
  22.         print(f"{key}: {value}")
  23. # 输出:
  24. # ip: 192.168.1.1
  25. # timestamp: 25/Dec/2023:10:00:00 +0000
  26. # method: GET
  27. # path: /index.html
  28. # protocol: HTTP/1.1
  29. # status: 200
  30. # size: 1024
复制代码

从文本中提取日期:
  1. import re
  2. def extract_dates(text):
  3.     # 匹配多种日期格式:YYYY-MM-DD, MM/DD/YYYY, DD Month YYYY, 等
  4.     patterns = [
  5.         r'\b(\d{4})-(\d{2})-(\d{2})\b',  # YYYY-MM-DD
  6.         r'\b(\d{2})/(\d{2})/(\d{4})\b',  # MM/DD/YYYY
  7.         r'\b(\d{1,2}) (\w+) (\d{4})\b'   # DD Month YYYY
  8.     ]
  9.    
  10.     dates = []
  11.     for pattern in patterns:
  12.         matches = re.findall(pattern, text)
  13.         for match in matches:
  14.             dates.append('-'.join(match))
  15.    
  16.     return dates
  17. # 测试
  18. text = """
  19. The event will take place on 2023-12-25.
  20. Registration deadline is 12/15/2023.
  21. Please submit your application by 25 December 2023.
  22. """
  23. dates = extract_dates(text)
  24. for date in dates:
  25.     print(date)
  26. # 输出:
  27. # 2023-12-25
  28. # 12-15-2023
  29. # 25-December-2023
复制代码

3. 搜索替换

正则表达式可以用于复杂的搜索和替换操作,特别是当需要基于模式匹配进行替换时。

格式化电话号码:
  1. import re
  2. def format_phone(phone):
  3.     # 移除所有非数字字符
  4.     digits = re.sub(r'\D', '', phone)
  5.    
  6.     # 根据数字长度格式化
  7.     if len(digits) == 10:
  8.         return re.sub(r'(\d{3})(\d{3})(\d{4})', r'(\1) \2-\3', digits)
  9.     elif len(digits) == 11 and digits[0] == '1':
  10.         return re.sub(r'(\d{1})(\d{3})(\d{3})(\d{4})', r'+\1 (\2) \3-\4', digits)
  11.     else:
  12.         return phone  # 无法识别的格式,原样返回
  13. # 测试
  14. phones = [
  15.     "1234567890",
  16.     "11234567890",
  17.     "(123) 456-7890",
  18.     "123-456-7890",
  19.     "123.456.7890",
  20.     "+1 123 456 7890"
  21. ]
  22. for phone in phones:
  23.     print(f"{phone} -> {format_phone(phone)}")
  24. # 输出:
  25. # 1234567890 -> (123) 456-7890
  26. # 11234567890 -> +1 (123) 456-7890
  27. # (123) 456-7890 -> (123) 456-7890
  28. # 123-456-7890 -> (123) 456-7890
  29. # 123.456.7890 -> (123) 456-7890
  30. # +1 123 456 7890 -> +1 (123) 456-7890
复制代码

清理HTML标签:
  1. import re
  2. def clean_html(html):
  3.     # 移除HTML标签,保留内容
  4.     clean = re.sub(r'<[^>]+>', '', html)
  5.     # 将多个空白字符替换为单个空格
  6.     clean = re.sub(r'\s+', ' ', clean)
  7.     return clean.strip()
  8. # 测试
  9. html = """
  10. <html>
  11. <body>
  12.     <h1>Sample Page</h1>
  13.     <p>This is a <b>sample</b> paragraph with <i>HTML</i> tags.</p>
  14.     <ul>
  15.         <li>Item 1</li>
  16.         <li>Item 2</li>
  17.     </ul>
  18. </body>
  19. </html>
  20. """
  21. print(clean_html(html))
  22. # 输出: Sample Page This is a sample paragraph with HTML tags. Item 1 Item 2
复制代码

高亮关键词:
  1. import re
  2. def highlight_keywords(text, keywords):
  3.     for keyword in keywords:
  4.         # 使用正则表达式匹配关键词,不区分大小写
  5.         pattern = re.compile(re.escape(keyword), re.IGNORECASE)
  6.         # 替换为高亮版本
  7.         text = pattern.sub(r'<mark>\g<0></mark>', text)
  8.     return text
  9. # 测试
  10. text = "Regular expressions are powerful tools for text processing. Learning regex can greatly improve your text processing capabilities."
  11. keywords = ["regex", "text", "processing"]
  12. highlighted = highlight_keywords(text, keywords)
  13. print(highlighted)
  14. # 输出: Regular expressions are powerful tools for <mark>text</mark> <mark>processing</mark>. Learning <mark>regex</mark> can greatly improve your <mark>text</mark> <mark>processing</mark> capabilities.
复制代码

4. 日志分析

正则表达式在日志分析中非常有用,可以快速提取关键信息、识别异常模式等。

统计HTTP状态码:
  1. import re
  2. from collections import Counter
  3. def analyze_http_status_codes(log_file_path):
  4.     # 匹配HTTP状态码
  5.     pattern = r'" (\d{3}) '
  6.    
  7.     status_counter = Counter()
  8.    
  9.     with open(log_file_path, 'r') as file:
  10.         for line in file:
  11.             match = re.search(pattern, line)
  12.             if match:
  13.                 status_code = match.group(1)
  14.                 status_counter[status_code] += 1
  15.    
  16.     return status_counter
  17. # 模拟使用
  18. # 假设有一个日志文件 'access.log'
  19. # status_counts = analyze_http_status_codes('access.log')
  20. # print(status_counts.most_common())
  21. # 演示数据
  22. log_lines = [
  23.     '192.168.1.1 - - [25/Dec/2023:10:00:00 +0000] "GET /index.html HTTP/1.1" 200 1024',
  24.     '192.168.1.2 - - [25/Dec/2023:10:01:00 +0000] "GET /about.html HTTP/1.1" 200 2048',
  25.     '192.168.1.3 - - [25/Dec/2023:10:02:00 +0000] "GET /contact.html HTTP/1.1" 404 512',
  26.     '192.168.1.4 - - [25/Dec/2023:10:03:00 +0000] "POST /submit HTTP/1.1" 500 256',
  27.     '192.168.1.5 - - [25/Dec/2023:10:04:00 +0000] "GET /products.html HTTP/1.1" 200 3072'
  28. ]
  29. pattern = r'" (\d{3}) '
  30. status_counter = Counter()
  31. for line in log_lines:
  32.     match = re.search(pattern, line)
  33.     if match:
  34.         status_code = match.group(1)
  35.         status_counter[status_code] += 1
  36. print("HTTP Status Code Counts:")
  37. for status, count in status_counter.most_common():
  38.     print(f"{status}: {count}")
  39. # 输出:
  40. # HTTP Status Code Counts:
  41. # 200: 3
  42. # 404: 1
  43. # 500: 1
复制代码

检测异常IP访问:
  1. import re
  2. from collections import defaultdict
  3. def detect_suspicious_ips(log_file_path, threshold=100):
  4.     # 提取IP地址
  5.     pattern = r'^(\S+)'
  6.    
  7.     ip_counter = defaultdict(int)
  8.    
  9.     with open(log_file_path, 'r') as file:
  10.         for line in file:
  11.             match = re.match(pattern, line)
  12.             if match:
  13.                 ip = match.group(1)
  14.                 ip_counter[ip] += 1
  15.    
  16.     # 返回访问次数超过阈值的IP
  17.     suspicious_ips = {ip: count for ip, count in ip_counter.items() if count > threshold}
  18.     return suspicious_ips
  19. # 演示数据
  20. log_lines = [
  21.     '192.168.1.1 - - [25/Dec/2023:10:00:00 +0000] "GET /index.html HTTP/1.1" 200 1024',
  22.     '192.168.1.1 - - [25/Dec/2023:10:01:00 +0000] "GET /about.html HTTP/1.1" 200 2048',
  23.     '192.168.1.2 - - [25/Dec/2023:10:02:00 +0000] "GET /contact.html HTTP/1.1" 404 512',
  24.     '192.168.1.1 - - [25/Dec/2023:10:03:00 +0000] "POST /submit HTTP/1.1" 500 256',
  25.     '192.168.1.3 - - [25/Dec/2023:10:04:00 +0000] "GET /products.html HTTP/1.1" 200 3072',
  26.     '192.168.1.1 - - [25/Dec/2023:10:05:00 +0000] "GET /services.html HTTP/1.1" 200 2048'
  27. ]
  28. pattern = r'^(\S+)'
  29. ip_counter = defaultdict(int)
  30. for line in log_lines:
  31.     match = re.match(pattern, line)
  32.     if match:
  33.         ip = match.group(1)
  34.         ip_counter[ip] += 1
  35. threshold = 2
  36. suspicious_ips = {ip: count for ip, count in ip_counter.items() if count > threshold}
  37. print(f"Suspicious IPs (more than {threshold} requests):")
  38. for ip, count in suspicious_ips.items():
  39.     print(f"{ip}: {count} requests")
  40. # 输出:
  41. # Suspicious IPs (more than 2 requests):
  42. # 192.168.1.1: 4 requests
复制代码

提取错误日志:
  1. import re
  2. def extract_error_logs(log_file_path):
  3.     # 匹配错误日志行
  4.     pattern = r'^.*\b(ERROR|FATAL|CRITICAL)\b.*$'
  5.    
  6.     error_logs = []
  7.    
  8.     with open(log_file_path, 'r') as file:
  9.         for line in file:
  10.             if re.match(pattern, line):
  11.                 error_logs.append(line.strip())
  12.    
  13.     return error_logs
  14. # 演示数据
  15. log_lines = [
  16.     '[2023-12-25 10:00:00] INFO: Server started successfully',
  17.     '[2023-12-25 10:01:00] INFO: User logged in',
  18.     '[2023-12-25 10:02:00] ERROR: Database connection failed',
  19.     '[2023-12-25 10:03:00] WARNING: Disk space running low',
  20.     '[2023-12-25 10:04:00] ERROR: Unable to process request',
  21.     '[2023-12-25 10:05:00] CRITICAL: System shutting down due to critical error'
  22. ]
  23. pattern = r'^.*\b(ERROR|FATAL|CRITICAL)\b.*$'
  24. error_logs = [line for line in log_lines if re.match(pattern, line)]
  25. print("Error Logs:")
  26. for log in error_logs:
  27.     print(log)
  28. # 输出:
  29. # Error Logs:
  30. # [2023-12-25 10:02:00] ERROR: Database connection failed
  31. # [2023-12-25 10:04:00] ERROR: Unable to process request
  32. # [2023-12-25 10:05:00] CRITICAL: System shutting down due to critical error
复制代码

五、常见问题与解决方案

1. 正则表达式不匹配预期内容

问题:正则表达式没有匹配到预期的内容,或者匹配到了不正确的内容。

解决方案:

1. 使用在线正则表达式测试工具(如regex101.com)进行调试
2. 逐步简化正则表达式,找出导致问题的部分
3. 确保正确转义特殊字符
4. 检查是否忽略了大小写(使用re.IGNORECASE标志)
5. 确保正确处理多行文本(使用re.MULTILINE标志)

示例:
  1. import re
  2. # 原始正则表达式可能不匹配
  3. text = "Email: user@example.com"
  4. pattern = r"email: (\w+@\w+\.\w+)"  # 不匹配,因为大小写不敏感
  5. # 修复后的正则表达式
  6. pattern_fixed = r"email: (\w+@\w+\.\w+)"  # 添加re.IGNORECASE标志
  7. match = re.search(pattern_fixed, text, re.IGNORECASE)
  8. if match:
  9.     print("Email found:", match.group(1))  # 输出: Email found: user@example.com
复制代码

2. 正则表达式性能问题

问题:正则表达式运行缓慢,尤其是在处理大文本时。

解决方案:

1. 避免嵌套量词,如(a+)+
2. 使用更具体的字符类代替.,如[^\n]代替.
3. 使用非捕获分组(?:...)代替捕获分组(...)
4. 使用原子分组或占有量词(如果支持)
5. 预编译正则表达式
6. 使用锚点^和$限制匹配范围

示例:
  1. import re
  2. import time
  3. # 性能较差的正则表达式
  4. text = "aaaaaaaaaaaaaaaaaaaa" * 1000
  5. pattern_bad = "(a+)+b"  # 嵌套量词,可能导致灾难性回溯
  6. # 性能较好的正则表达式
  7. pattern_good = "a+b"  # 简化量词
  8. # 测试性能
  9. start_time = time.time()
  10. re.search(pattern_bad, text)
  11. print("Bad pattern time:", time.time() - start_time)
  12. start_time = time.time()
  13. re.search(pattern_good, text)
  14. print("Good pattern time:", time.time() - start_time)
复制代码

3. 处理复杂文本结构

问题:需要匹配复杂的文本结构,如嵌套括号、HTML标签等。

解决方案:

1. 对于简单的嵌套结构,可以使用递归正则表达式(如果支持)
2. 对于复杂的嵌套结构,考虑使用专门的解析器
3. 分步骤处理,先提取外层结构,再处理内层结构

示例:
  1. import re
  2. # 匹配简单的嵌套括号
  3. text = "func(a(b)c)d"
  4. pattern = r"\((?:[^()]|\((?:[^()]|\([^()]*\))*\))*\)"  # 递归匹配嵌套括号
  5. matches = re.findall(pattern, text)
  6. print("Nested parentheses:", matches)  # 输出: Nested parentheses: ['(a(b)c)']
  7. # 对于更复杂的HTML,建议使用专门的HTML解析器
  8. from bs4 import BeautifulSoup
  9. html = "<div><p>Hello <span>world</span></p></div>"
  10. soup = BeautifulSoup(html, 'html.parser')
  11. print("Text content:", soup.get_text())  # 输出: Text content: Hello world
复制代码

4. 跨平台兼容性问题

问题:正则表达式在不同编程语言或工具中的行为不一致。

解决方案:

1. 了解不同实现之间的差异
2. 使用基本的、广泛支持的特性
3. 避免使用特定于某个实现的高级特性
4. 在目标平台上测试正则表达式

示例:
  1. # JavaScript支持的后行断言在Python中可能不支持
  2. # Python需要使用固定长度的模式
  3. # JavaScript代码
  4. # const pattern = /(?<=\$)\d+/;  // 匹配前面是$符号的数字
  5. # Python代码(需要修改)
  6. text = "Price: $100, $200, $300"
  7. pattern = r"(?<=\$)\d+"  # Python支持固定长度的后行断言
  8. matches = re.findall(pattern, text)
  9. print("Numbers after $:", matches)  # 输出: Numbers after $: ['100', '200', '300']
复制代码

六、学习资源与工具推荐

1. 在线工具

1. Regex101(https://regex101.com/)提供正则表达式测试、调试和解释支持多种编程语言(Python、JavaScript、PCRE等)提供匹配过程的可视化
2. 提供正则表达式测试、调试和解释
3. 支持多种编程语言(Python、JavaScript、PCRE等)
4. 提供匹配过程的可视化
5. RegExr(https://regexr.com/)直观的正则表达式测试工具包含详细的参考资料和示例支持实时高亮显示匹配结果
6. 直观的正则表达式测试工具
7. 包含详细的参考资料和示例
8. 支持实时高亮显示匹配结果
9. Debuggex(https://www.debuggex.com/)提供正则表达式的可视化表示支持JavaScript和Python可以生成正则表达式的 Railroad 图
10. 提供正则表达式的可视化表示
11. 支持JavaScript和Python
12. 可以生成正则表达式的 Railroad 图

Regex101(https://regex101.com/)

• 提供正则表达式测试、调试和解释
• 支持多种编程语言(Python、JavaScript、PCRE等)
• 提供匹配过程的可视化

RegExr(https://regexr.com/)

• 直观的正则表达式测试工具
• 包含详细的参考资料和示例
• 支持实时高亮显示匹配结果

Debuggex(https://www.debuggex.com/)

• 提供正则表达式的可视化表示
• 支持JavaScript和Python
• 可以生成正则表达式的 Railroad 图

2. 书籍推荐

1. 《精通正则表达式》(Mastering Regular Expressions)作者:Jeffrey E.F. Friedl被誉为正则表达式领域的”圣经”深入讲解正则表达式的原理和应用
2. 作者:Jeffrey E.F. Friedl
3. 被誉为正则表达式领域的”圣经”
4. 深入讲解正则表达式的原理和应用
5. 《正则表达式必知必会》(Regular Expressions Cookbook)作者:Jan Goyvaerts, Steven Levithan提供大量实用的正则表达式示例涵盖多种编程语言
6. 作者:Jan Goyvaerts, Steven Levithan
7. 提供大量实用的正则表达式示例
8. 涵盖多种编程语言
9. 《Python正则表达式实战》(Python Regular Expressions: The Complete Guide)专注于Python中的正则表达式应用包含大量实际案例和代码示例
10. 专注于Python中的正则表达式应用
11. 包含大量实际案例和代码示例

《精通正则表达式》(Mastering Regular Expressions)

• 作者:Jeffrey E.F. Friedl
• 被誉为正则表达式领域的”圣经”
• 深入讲解正则表达式的原理和应用

《正则表达式必知必会》(Regular Expressions Cookbook)

• 作者:Jan Goyvaerts, Steven Levithan
• 提供大量实用的正则表达式示例
• 涵盖多种编程语言

《Python正则表达式实战》(Python Regular Expressions: The Complete Guide)

• 专注于Python中的正则表达式应用
• 包含大量实际案例和代码示例

3. 学习网站

1. MDN Web Docs - Regular Expressions提供JavaScript中正则表达式的详细文档包含丰富的示例和教程网址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
2. 提供JavaScript中正则表达式的详细文档
3. 包含丰富的示例和教程
4. 网址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
5. Python官方文档 - re模块Python正则表达式的官方文档包含完整的API参考和示例网址:https://docs.python.org/3/library/re.html
6. Python正则表达式的官方文档
7. 包含完整的API参考和示例
8. 网址:https://docs.python.org/3/library/re.html
9. Regular-Expressions.info专注于正则表达式的教程网站涵盖基础到高级的各种主题网址:https://www.regular-expressions.info/
10. 专注于正则表达式的教程网站
11. 涵盖基础到高级的各种主题
12. 网址:https://www.regular-expressions.info/

MDN Web Docs - Regular Expressions

• 提供JavaScript中正则表达式的详细文档
• 包含丰富的示例和教程
• 网址:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Python官方文档 - re模块

• Python正则表达式的官方文档
• 包含完整的API参考和示例
• 网址:https://docs.python.org/3/library/re.html

Regular-Expressions.info

• 专注于正则表达式的教程网站
• 涵盖基础到高级的各种主题
• 网址:https://www.regular-expressions.info/

4. 练习平台

1. HackerRank - Regex提供正则表达式的编程挑战从基础到高级的难度递增网址:https://www.hackerrank.com/domains/regex
2. 提供正则表达式的编程挑战
3. 从基础到高级的难度递增
4. 网址:https://www.hackerrank.com/domains/regex
5. Codewars - Regex Kata社区驱动的编程挑战包含各种正则表达式问题网址:https://www.codewars.com/?language=python&tags=regex
6. 社区驱动的编程挑战
7. 包含各种正则表达式问题
8. 网址:https://www.codewars.com/?language=python&tags=regex
9. Exercism - Regex Track提供导师指导的学习路径包含正则表达式的练习和反馈网址:https://exercism.org/tracks/regex
10. 提供导师指导的学习路径
11. 包含正则表达式的练习和反馈
12. 网址:https://exercism.org/tracks/regex

HackerRank - Regex

• 提供正则表达式的编程挑战
• 从基础到高级的难度递增
• 网址:https://www.hackerrank.com/domains/regex

Codewars - Regex Kata

• 社区驱动的编程挑战
• 包含各种正则表达式问题
• 网址:https://www.codewars.com/?language=python&tags=regex

Exercism - Regex Track

• 提供导师指导的学习路径
• 包含正则表达式的练习和反馈
• 网址:https://exercism.org/tracks/regex

七、总结

正则表达式是一种强大而灵活的文本处理工具,掌握它可以极大地提升文本搜索和处理能力。本文从正则表达式的基础语法开始,逐步介绍了进阶技巧、不同编程语言中的应用以及实战案例,帮助读者全面了解正则表达式的使用方法。

通过学习正则表达式,你可以:

1. 高效地验证用户输入的数据格式
2. 快速从文本中提取所需信息
3. 批量处理文本搜索和替换任务
4. 分析日志文件,提取关键数据
5. 处理复杂的文本匹配需求

虽然正则表达式可能看起来复杂,但通过不断练习和应用,你将能够熟练掌握这一技能,并在日常工作和学习中发挥其巨大作用。记住,正则表达式是一项需要实践的技能,多写多练是提高的最佳途径。

希望本文能够帮助你掌握正则表达式技巧,让你的文本搜索能力提升百倍!
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则