您好,欢迎访问一九零五行业门户网

Python程序提取字符串直到第一个非字母数字字符

python 字符串是表示信息或数据的字符序列。普通字符串可以包含用单引号或双引号括起来的各种字符,但字母数字字符串仅包含数字和字母。 字母数字和非字母数字字符串都在各种场景中使用和应用,包括密码保护、数据处理和验证、格式化等。
可以识别和提取特定模式。我们还可以使用这些类型的字符串提供不同的组合。我们将根据这些字符串执行操作。我们的任务是提取字符串,直到遇到第一个非字母数字字符。
理解问题在遇到非字母数字字符之前,我们必须从原始字符串中提取子字符串。让我们通过一个例子来理解这一点。
输入输出场景让我们考虑一个具有以下值的字典 -
input: inp_str = sales18@22!roam
给定的字符串由字母、数字和特殊字符组成。一旦遇到非字母数字字符,我们就必须检索子字符串。
output: sales18
我们可以看到,从原始字符串中返回了一个子字符串“sales18”,因为在此之后遇到了非字母数字字符,即“@”。现在我们已经理解了问题陈述,让我们讨论一些解决方案。
使用迭代这是根据提供的条件提取字符串的基本且更简单的方法。我们将传递一个字符串并创建一个新变量,该变量将存储所有字母数字字符,即字母(大写和小写)和数字。之后,我们将遍历原始字符串并迭代每个字符。
我们将建立一个条件来检查原始字符串中的字符是否是字母数字。一旦遇到非字母数字字符,循环将中断并返回子字符串。
示例以下是提取字符串直到第一个非字母数字字符的示例 -
inp_str = sales18@22roamprint(fthe original string is: {inp_str})exstr = alphanum = abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890for x in inp_str: if x not in alphanum: break else: exstr += xprint(fthe extracted string till 1st non-alphanumeric character: {exstr})
输出the original string is: sales18@22roamthe extracted string till 1st non-alphanumeric character: sales18

使用正则表达式模块 + search()regex 模块或“re”模块是一个强大的编程工具,用于搜索和匹配模式。这些模式以独特的表达式的形式传递。使用此模块,我们将检测原始字符串中的非字母数字模式并检索第一次遇到的序列。我们使用“search()”函数在字符串中搜索由表达式“\w+”表示的非字母数字模式。
“\w”表示非字母数字类,“+”设置非字母数字字符的连续匹配逻辑。 “.start()”方法返回匹配子字符串的起始索引,该索引值将用于检索所需的子字符串。
示例以下是一个示例 -
import reinp_str = sales18@22roamprint(fthe original string is: {inp_str})exstr = re.search(r\w+, inp_str).start()print(fthe 1st non-alphanumeric character is encountered at: {exstr})exstr = inp_str[ : exstr]print(fthe extracted string till 1st non-alphanumeric character: {exstr})
输出the original string is: sales18@22roamthe 1st non-alphanumeric character is encountered at: 7the extracted string till 1st non-alphanumeric character: sales18

使用 regex 模块 + findall()这是提取字符串直到遇到第一个非字母数字字符的另一种方法。在这种方法中,我们将使用 re 模块中的“findall()”函数来查找所有出现的由字母数字字符组成的子字符串。
将获得匹配子字符串的列表,我们将使用“0”索引值检索第一个子字符串。我们将使用正则表达式:“[\da-za-z]*”,它表示一行中的零个或多个字母数字字符。
正则表达式符号“\d”匹配0到9之间的任何数字,“a-z”匹配a到z之间的任何大写字母,“a-z” 匹配 a 到 z 之间的任何小写字母。
示例以下是一个示例 -
import reinp_str = sales18@22roamprint(fthe original string is: {inp_str})exstr = re.findall(r[\da-za-z]*, inp_str)[0]print(fthe extracted string till 1st non-alphanumeric character: {exstr})
输出the original string is: sales18@22roamthe extracted string till 1st non-alphanumeric character: sales18

使用 isalnum() 方法在这种方法中,我们将迭代原始字符串中每个字符的索引,并建立一个条件来检查索引“x”处的字符是否不是字母数字。这是在“isalnum()”方法的帮助下完成的,该方法确定字符串的字母数字性质。之后,我们将使用列表切片来提取字符串,直到第一个字母数字字符。
示例以下是一个示例 -
inp_str = sales18@22roamprint(fthe original string is: {inp_str})for x in range(len(inp_str)): if not inp_str[x].isalnum(): exstr = inp_str[:x] print(fthe 1st non-alphanumeric character is encountered at: {x}) break else: exstr = inp_strprint(fthe extracted string till 1st non-alphanumeric character: {exstr})
输出the original string is: sales18@22roamthe 1st non-alphanumeric character is encountered at: 7the extracted string till 1st non-alphanumeric character: sales18

结论在本文中,我们讨论了一些高效且优化的解决方案,用于在遇到第一个非字母数字字符时从字符串中提取子字符串。我们了解简单和粗暴的解决方案以及先进和优化的解决方案。我们使用正则表达式模块并使用其“search()”和“findall()”函数来提取相关字符串。最后,我们讨论了另一种基于列表切片的解决方案,其中涉及使用“isalnum()”方法。
以上就是python程序提取字符串直到第一个非字母数字字符的详细内容。
其它类似信息

推荐信息