正则表达式

1.小写化

将文本转换为小写 text.lower()

2.去除标点符号

使用正则表达式去除文本中的标点符号。

3.去除多余空格

将多个空格替换为单个空格,并去除首尾空格 ' '.join(text.split())

4.去除数字

用正则表达式去除文本中的数字。

5.替换缩写

将常见的缩写替换为完整形式。text = text.replace("I'm","I am")

结合以上方法,对文本进行系统的正则化处理

import re

from nltk.corpus import stopwords

from nltk.tokenize import word_tokenize

from nltk.stem import WordNetLemmatizer

from bs4 import BeautifulSoup

def normalize_text(text):

    # 移除HTML标签

    soup = BeautifulSoup(text, "html.parser")

    text = soup.get_text()

    # 转换为小写

    text = text.lower()

    # 去除标点符号

    text = re.sub(r'[^\w\s]', ' ', text)

    # 去除多余空格

    text = ' '.join(text.split())

    # 去除数字

    text = re.sub(r'\d+', '', text)

    # 去除停用词

    stop_words = set(stopwords.words('english'))

    word_tokens = word_tokenize(text)

    text = ' '.join(word for word in word_tokens if word not in stop_words)

    # 词形还原

    lemmatizer = WordNetLemmatizer()

    word_tokens = word_tokenize(text)

    text = ' '.join(lemmatizer.lemmatize(word) for word in word_tokens)

    return text

# 示例文本

text = "<html><body><h1>Example Title</h1><p>This is an example sentence, with numbers 123 and HTML tags!</p></body></html>"

normalized_text = normalize_text(text)

print(normalized_text)  # 输出: "example title example sentence number html tag"


正则表达式的常见用法

#### 1. 匹配任意字符

`.`: 匹配任意单个字符(除了换行符)。

import re

result = re.findall(r'a.b', 'aab abb acb adb')

print(result)  # 输出: ['aab', 'abb', 'acb', 'adb']

#### 2. 字符类

- `[abc]`: 匹配字符 'a', 'b' 或 'c'。

- `[a-z]`: 匹配任何小写字母。

- `[A-Z]`: 匹配任何大写字母。

- `[0-9]`: 匹配任何数字。

- `[^abc]`: 匹配除 'a', 'b', 'c' 之外的任意字符。

result = re.findall(r'[a-c]', 'abcxyz')

print(result)  # 输出: ['a', 'b', 'c']

#### 3. 预定义字符类

- `\d`: 匹配任何数字,等价于 `[0-9]`。

- `\D`: 匹配任何非数字字符。

- `\w`: 匹配任何字母、数字或下划线,等价于 `[a-zA-Z0-9_]`。

- `\W`: 匹配任何非字母、数字、下划线字符。

- `\s`: 匹配任何空白字符(空格、制表符、换行符)。

- `\S`: 匹配任何非空白字符。

result = re.findall(r'\d+', 'There are 123 apples and 45 bananas')

print(result)  # 输出: ['123', '45']

#### 4. 边界匹配

- `^`: 匹配字符串的开头。

- `$`: 匹配字符串的结尾。

- `\b`: 匹配单词边界。

- `\B`: 匹配非单词边界。

```python

result = re.findall(r'\bword\b', 'a word in a sentence')

print(result)  # 输出: ['word']

#### 5. 量词

- `*`: 匹配前面的字符零次或多次。

- `+`: 匹配前面的字符一次或多次。

- `?`: 匹配前面的字符零次或一次。

- `{n}`: 匹配前面的字符恰好 n 次。

- `{n,}`: 匹配前面的字符至少 n 次。

- `{n,m}`: 匹配前面的字符至少 n 次,但不超过 m 次。

result = re.findall(r'\d{2,4}', '123 1234 12345')

print(result)  # 输出: ['123', '1234', '1234']

#### 6. 分组和捕获

- `()`: 用于分组和捕获匹配的子字符串。

result = re.findall(r'(\d+)-(\d+)-(\d+)', '123-456-7890')

print(result)  # 输出: [('123', '456', '7890')]

#### 7. 或运算

- `|`: 表示“或”运算,匹配符号前后任意一个正则表达式。

result = re.findall(r'apple|orange', 'I like apple and orange')

print(result)  # 输出: ['apple', 'orange']

#### 8. 转义字符

- `\`: 用于转义元字符,使其作为普通字符使用。

result = re.findall(r'\$[0-9]+', 'The price is $100')

print(result)  # 输出: ['$100']

### 综合示例

结合多个正则表达式操作来处理文本。

import re

text = "Hello, World! This is a test. 123-456-7890. Email: test@example.com"

# 1. 小写化

text = text.lower()

# 2. 去除标点符号

text = re.sub(r'[^\w\s]', ' ', text)

# 3. 去除多余空格

text = ' '.join(text.split())

# 4. 去除数字

text = re.sub(r'\d+', '', text)

# 5. 找出所有单词

words = re.findall(r'\b\w+\b', text)

print(text)  # 输出: "hello world this is a test email test example com"

print(words)  # 输出: ['hello', 'world', 'this', 'is', 'a', 'test', 'email', 'test', 'example', 'com']

通过掌握这些常见的正则表达式操作,您可以有效地处理和规范化文本数据。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容