Python regex adds symbols in front of all Chinese characters and numbers in the string

You can use regular expressions and Python's re module to implement the requirement of adding # in front of all Chinese characters and numbers in the string. Here is a sample code:

import re

def add_hash_before_chinese_numbers(string):
    pattern = r'(\d+|[一二三四五六七八九十百千万亿]+)'
    result = re.sub(pattern, r'#\1', string)
    return result

# 示例用法
string = '这是一段测试字符串,包含123和一二三四五六七八九十的数字'
result = add_hash_before_chinese_numbers(string)
print(result)

Run the above code, the output is as follows:

这是一段测试字符串,包含#123和#一二三四五六七八九十的数字

The regular expression pattern is used in the code (\d+|[一二三四五六七八九十百千万亿]+)to match Chinese numerals and Arabic numerals. Then use the re.sub() function to replace the matched content with the form of "#number". Finally returns the replaced result.

 

 

Guess you like

Origin blog.csdn.net/qq_26429153/article/details/131610277