简体   繁体   English

正则表达式删除多个空格后的字符

[英]Regex to remove characters after multiple white spaces

Hi all I've got and input strings:大家好,我有并输入字符串:

list_of_strings = ["apple", "orange ca", "pear  sa", "banana    sth"]

And I want to remove everything after multiple white spaces (more than 1), so end result is:我想在多个空格(超过 1 个)之后删除所有内容,所以最终结果是:

final_list_of_strings = ["apple", "orange ca", "pear", "banana"]

I've tried regex:我试过正则表达式:

import re

regex_expression = r"(.*\s?)(\s{2,}.*)"

for name in list_of_strings:
    regex_matching_groups = re.findall(regex_expression, name)
    if regex_matching_groups:
    name = regex_matching_groups[0][0]
    

but fails on multiple spaces ... Thank you for help!但在多个空间失败......谢谢你的帮助!

You can use re.sub in a list comprehension:您可以在列表理解中使用re.sub

import re
list_of_strings = ["apple", "orange ca", "pear  sa", "banana    sth"]
list_of_strings = [re.sub(r'\s{2}.*', '', x, flags=re.S) for x in list_of_strings]
print(list_of_strings)
# -> ['apple', 'orange ca', 'pear', 'banana']

See the Python demo .请参阅Python 演示

The \\s{2}.* regex matches two whitespace chars and then the rest of the string (even if there are line break chars due to re.S flag). \\s{2}.*正则表达式匹配两个空白字符,然后匹配字符串的其余部分(即使由于re.S标志存在re.S )。

using a regular expression find the first word and optionally a second word with only one space between使用正则表达式查找第一个单词和可选的第二个单词,它们之间只有一个空格

list_of_strings = ["apple", "orange ca", "pear  sa", "banana    sth"]

my_list=[]
def find_phrase(list_of_strings):
    for string in list_of_strings:
        matches=re.findall(r"(\w+)( \w+)*", string)
        if len(matches)>0:
            my_list.append("".join([matches[0][0],matches[0][1]]))
    return my_list
        
 print(find_phrase(list_of_strings))

output:输出:

['apple', 'orange ca', 'pear', 'banana']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM