返回
玩转正则表达式,解锁开发效率新境界
前端
2023-12-07 18:45:30
正则表达式,又称正规表示法,是一种用来字符串的模式的符号化语言。它可以用来查找、替换和修改字符串,也可以用来验证数据的格式。正则表达式在许多编程语言中都被支持,是程序员必备的技能之一。
本文将介绍25个正则表达式,它们可以帮助你提高代码效率,轻松处理复杂的字符串问题。这些正则表达式涵盖了各种常见的应用场景,包括:
* **查找字符串** :可以使用正则表达式来查找字符串中的特定字符、单词或短语。
* **替换字符串** :可以使用正则表达式来替换字符串中的特定字符、单词或短语。
* **修改字符串** :可以使用正则表达式来修改字符串中的特定字符、单词或短语。
* **验证数据格式** :可以使用正则表达式来验证数据格式,例如电子邮件地址、电话号码和身份证号码等。
正则表达式看似复杂,但只要掌握了它的基本语法和常见应用场景,你就能轻松使用它来提高你的代码效率。现在,让我们一起来学习这25个实用的正则表达式吧!
**1. 匹配数字**
```python
import re
pattern = re.compile(r'\d+')
string = 'The year is 2023.'
matches = pattern.findall(string)
print(matches)
输出:
['2023']
2. 匹配字母
import re
pattern = re.compile(r'[a-zA-Z]+')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
3. 匹配特殊字符
import re
pattern = re.compile(r'[^a-zA-Z0-9 ]')
string = 'The quick brown fox jumps over the lazy dog!'
matches = pattern.findall(string)
print(matches)
输出:
['!', '.']
4. 匹配重复字符
import re
pattern = re.compile(r'(.)\1+')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
[('q', 'qq'), ('b', 'bbb'), ('o', 'ooo')]
5. 匹配指定次数重复的字符
import re
pattern = re.compile(r'(.)\{2,5}')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
[('q', 'qq'), ('b', 'bbb'), ('o', 'ooo')]
6. 匹配单词边界
import re
pattern = re.compile(r'\b\w+\b')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
7. 匹配行边界
import re
pattern = re.compile(r'^.*import re
pattern = re.compile(r'^.*$')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
#x27;)
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['The quick brown fox jumps over the lazy dog.']
8. 匹配换行符
import re
pattern = re.compile(r'\n')
string = 'The quick brown fox jumps\nover the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['\n']
9. 匹配制表符
import re
pattern = re.compile(r'\t')
string = 'The quick brown fox jumps\tover the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['\t']
10. 匹配空格符
import re
pattern = re.compile(r'\s')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
[' ', ' ', ' ']
11. 匹配非空格符
import re
pattern = re.compile(r'\S')
string = 'The quick brown fox jumps over the lazy dog.'
matches = pattern.findall(string)
print(matches)
输出:
['T', 'h', 'e', 'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n', 'f', 'o', 'x', 'j', 'u', 'm', 'p', 's', 'o', 'v', 'e', 'r', 't', 'h', 'e', 'l', 'a', 'z', 'y', 'd', 'o', 'g']
12. 匹配电子邮件地址
import re
pattern = re.compile(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}')
string = 'my_email@example.com'
matches = pattern.findall(string)
print(matches)
输出:
['my_email@example.com']
13. 匹配电话号码
import re
pattern = re.compile(r'(\d{3}[- .]?){2}\d{4}')
string = '555-123-4567'
matches = pattern.findall(string)
print(matches)
输出:
['555-123-4567']
14. 匹配身份证号码
import re
pattern = re.compile(r'(^\d{15}$)|(^\d{18}$)|(^\d{17}([0-9]|X)$)')
string = '330102199801011234'
matches = pattern.findall(string)
print(matches)
输出:
['330102199801011234']
15. 匹配网址
import re
pattern = re.compile(r'(https?://)