目录
1、字符串的去重
2、正则表达式
1、字符串的去重
在Python中,你可以使用不同的方法对字符串进行去重,具体取决于你的需求。以下是一些可能的方法:
-
使用集合(Set):
利用集合的特性,集合中的元素是唯一的。你可以将字符串转换为集合,然后再将其转回字符串。
original_string = "abracadabra" unique_chars = ''.join(set(original_string)) print("String with unique characters:", unique_chars)
注意:这种方法无法保持原始字符串中字符的顺序。
-
使用循环和条件判断:
通过遍历原始字符串,逐个字符添加到新的字符串中,但只添加之前没有添加过的字符。
original_string = "abracadabra" unique_chars = ""for char in original_string:if char not in unique_chars:unique_chars += charprint("String with unique characters:", unique_chars)
这种方法会保持原始字符串中字符的顺序。
-
使用字典保持顺序:
利用字典的键的唯一性,逐个遍历字符,将其作为字典的键添加,最后取字典的键即可。
original_string = "abracadabra" unique_chars = ''.join(dict.fromkeys(original_string))print("String with unique characters:", unique_chars)
这种方法也会保持原始字符串中字符的顺序。
2、正则表达式
正则表达式(Regular Expressions,简称为Regex或RegExp)是一种强大而灵活的文本匹配工具,用于在字符串中搜索、匹配和替换文本。正则表达式由一系列字符和操作符构成,形成一个搜索模式。
以下是一些常见的正则表达式元字符:
。
: 匹配任意单个字符,除了换行符。import repattern = r"H.t" # 匹配 "Hat"、"Hot" 等 text = "Hat is hot." result = re.findall(pattern, text) print(result)
-
*
: 匹配前面的元字符零次或多次。pattern = r"ab*" # 匹配 "a", "ab", "abb", "abbb", 等 text = "abb" result = re.findall(pattern, text) print(result)
-
+
: 匹配前面的元字符一次或多次。pattern = r"ab+" # 匹配 "ab", "abb", "abbb", 等,但不匹配 "a" text = "abb" result = re.findall(pattern, text) print(result)
-
?
: 匹配前面的元字符零次或一次。pattern = r"ab?" # 匹配 "a" 或 "ab" text = "abb" result = re.findall(pattern, text) print(result)
-
[]
: 匹配方括号中的任意一个字符。pattern = r"[aeiou]" # 匹配任何一个元音字母 text = "Hello" result = re.findall(pattern, text) print(result)
-
^
和$
:^
匹配字符串的开头。$
匹配字符串的结尾。pattern = r"^Hello" # 匹配以 "Hello" 开头的字符串 text = "Hello, World!" result = re.findall(pattern, text) print(result)
-
\
: 转义字符,用于匹配特殊字符。pattern = r"\d+" # 匹配一个或多个数字 text = "There are 123 apples." result = re.findall(pattern, text) print(result)
re模块:
re
模块是 Python 中用于处理正则表达式的标准库模块。它提供了一组功能,允许你在字符串中进行模式匹配、搜索和替换等操作。以下是一些 模块中常用的函数:
re.match(pattern, string)
: 从字符串的开头匹配模式,如果匹配成功返回一个匹配对象,否则返回 。None
import repattern = r"Hello" text = "Hello, World!"match_result = re.match(pattern, text) if match_result:print("Match found:", match_result.group()) else:print("No match.")
-
re.search(pattern, string)
: 在整个字符串中搜索模式,如果匹配成功返回一个匹配对象,否则返回 。None
pattern = r"World" text = "Hello, World!"search_result = re.search(pattern, text) if search_result:print("Match found:", search_result.group()) else:print("No match.")
-
re.findall(pattern, string)
: 返回字符串中所有与模式匹配的子串组成的列表。pattern = r"\b\w+\b" # 匹配单词 text = "Hello, World! This is a sample text."findall_result = re.findall(pattern, text) print("All matches:", findall_result)
-
re.finditer(pattern, string)
: 返回一个迭代器,迭代器中的每个元素都是一个匹配对象。pattern = r"\b\w+\b" # 匹配单词 text = "Hello, World! This is a sample text."finditer_result = re.finditer(pattern, text) for match in finditer_result:print("Match found:", match.group())
-
re.sub(pattern, replacement, string)
: 替换字符串中与模式匹配的部分。pattern = r"\b\w+\b" # 匹配单词 text = "Hello, World! This is a sample text."replaced_text = re.sub(pattern, "REPLACED", text) print("Replaced text:", replaced_text)
pattern
:一个正则表达式模式,用于匹配字符串中的部分内容。replacement
:用于替换匹配部分的字符串。string
:待处理的字符串,对其进行替换操作。 -
re.split
(pattern, string)
:拆分字符串。import repattern = r"\s" # 以空格为分隔符 text = "Hello World! This is a sample text."result = re.split(pattern, text) print(result)