Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
识别python中的交替大写和小写字符_Python_Regex_String_Python 2.7_Python 3.x - Fatal编程技术网

识别python中的交替大写和小写字符

识别python中的交替大写和小写字符,python,regex,string,python-2.7,python-3.x,Python,Regex,String,Python 2.7,Python 3.x,我有如下数据: data['word'] 1 Word1 2 WoRdqwertf point 3 lengthy word 4 AbCdEasc 5 Not to be filtered 6 GiBeRrIsH 7 zSxDcFvGnnn 我想找出字符串中交替出现的大写和小写字母,并删除包含这些单词的行。例如,如果我们在这里看到,WoRdqwertf、AbCdEasc、GiBeRrIsH、zSxDcFvGnnn具有交替字符,我需要删除这些字符 这里的要点是,不应该删除包含Wo

我有如下数据:

data['word']

1  Word1
2  WoRdqwertf point
3  lengthy word
4  AbCdEasc
5  Not to be filtered
6  GiBeRrIsH
7  zSxDcFvGnnn
我想找出字符串中交替出现的大写和小写字母,并删除包含这些单词的行。例如,如果我们在这里看到,
WoRdqwertf、AbCdEasc、GiBeRrIsH、zSxDcFvGnnn
具有交替字符,我需要删除这些字符

这里的要点是,不应该删除包含
Word1
的第一行,因为它只有一个大写字母和一个小大写字母。我只想在有caps,small,caps排列或small,caps,small排列时删除行。我在这里的输出应该是

data['word']

1  Word1
3  lengthy word
5  Not to be filtered

有人能帮我解决这个问题吗?你可以使用字符串方法。冗长->

一行->

输出:

['Word1', 'point', 'lengthy word', 'Not to be filtered']

您也可以使用
过滤器

data=['Word1','WoRdqwertf point','lengthy word','AbCdEasc','Not to be filtered','GiBeRrIsH','zSxDcFvGnnn']
str_list = filter(lambda item: (item[0].isupper() and item[1:].lower()==item[1:]) or item.islower(), data)
print(list(str_list))
#['Word1', 'lengthy word', 'Not to be filtered'] 

筛选器将只添加小写的
item.islower()
项和仅以大写开头的
(item[0].isupper()和item[1:][.lower()==item[1:])
您可以使用正则表达式
^(?:\w[a-z0-9]*(?:|$)*$

data = ['Word1','WoRdqwertf point','lengthy word','AbCdEasc', 'Not to be filtered','GiBeRrIsH', 'zSxDcFvGnnn']
import re
for line in data:
    if re.search(r'^(?:\w[a-z0-9]*(?: |$))*$', line):
         print (line)

请参见正则表达式解决方案:

import re

rx = re.compile(r'(?=.*(?:\b[A-Z][a-z\d]+\b)|^[a-z ]+$).+')

lst = ['Word1', 'WoRdqwertf point', 'lengthy word', 'AbCdEasc', 'Not to be filtered', 'GiBeRrIsH', 'zSxDcFvGnnn']
new_list = [item \
            for item in lst \
            if rx.match(item)]

print(new_list)
# ['Word1', 'lengthy word', 'Not to be filtered']

你试过什么吗?试一下
re.search(r'[a-z][a-z][a-z]|[a-z][a-z][a-z]',x)
@depperm我不知道怎么试。谢谢。我可以要求删除该单词而不是整行吗?例如,在WoRdqwertf point中,将其单独表示,在AbCdEasc中,将其表示为空。嗯,这一要求不在您的原始帖子中,我将在稍后更新。很抱歉刚才想到了这一点。也许
re.sub(r'\s*\b(?=\w*(?:[a-z][a-z]+[a-z][a-z]+[a-z]))\w+,'',节)
@海门
data = ['Word1','WoRdqwertf point','lengthy word','AbCdEasc', 'Not to be filtered','GiBeRrIsH', 'zSxDcFvGnnn']
import re
for line in data:
    if re.search(r'^(?:\w[a-z0-9]*(?: |$))*$', line):
         print (line)
import re

rx = re.compile(r'(?=.*(?:\b[A-Z][a-z\d]+\b)|^[a-z ]+$).+')

lst = ['Word1', 'WoRdqwertf point', 'lengthy word', 'AbCdEasc', 'Not to be filtered', 'GiBeRrIsH', 'zSxDcFvGnnn']
new_list = [item \
            for item in lst \
            if rx.match(item)]

print(new_list)
# ['Word1', 'lengthy word', 'Not to be filtered']