简体   繁体   English

python 正则表达式查找小写字母后跟大写字母

[英]python regex find lowercase followed by capital letter

I feel like I have to apologize in advance for this one, but I've searched for answers and they seem to tell me what I'm doing is correct.我觉得我必须为此提前道歉,但我已经搜索了答案,他们似乎告诉我我所做的是正确的。

I'm trying to set a DataFrame column to True if another column has instances of a lowercase letter immediately followed by an uppercase letter.如果另一列有一个小写字母紧跟一个大写字母的实例,我正在尝试将 DataFrame 列设置为 True。

What I tried was this:我试过的是:

    cities['multiteam'] = cities['team'].apply(lambda x: pd.notna(re.search(r'[A][a]',x)))

That's setting all the results to False, so I figured maybe I was doing something wrong with my lambda function and I made the following to debug just the re.search() part:这将所有结果设置为 False,所以我想我的 lambda function 可能做错了,我做了以下内容来调试 re.search() 部分:

cities['multiteam'] = pd.notna(re.search(r'[a][A]','OneTwo'))

That's also setting all the results to False.这也将所有结果设置为 False。 And there I'm stuck.我被困在那里了。

The following code is useful only to look for a letter 'A' followed by the lower case 'a'.以下代码仅适用于查找后跟小写字母“a”的字母“A”。

cities['multiteam'] = cities['team'].apply(lambda t: pd.notna(re.search(r'[A][a]',t)))

You may need to change it if you want to check it for all letters.如果您想检查所有字母,您可能需要更改它。 Maybe replace that line with something like this:也许用这样的东西替换那行:

cities['multiteam'] = cities['team'].apply(lambda t: pd.notna(re.search(r'[A-Z][a-z]',t)))

You should never to apologise about asking questions.你永远不应该为提问而道歉。 Using apply is quite slow, try and use the str.contains which can accept a regex pattern.使用apply非常慢,请尝试使用可以接受正则表达式模式的str.contains

cities.assign(multiteam=cities.team.str.contains('[a-z][A-Z]'))

The assign above is pandas new recommend way of assigning columns.上面的assign是pandas新推荐的列赋值方式。

The str.contains works with regex and fixed strings, much faster than apply . str.contains适用于正则表达式和固定字符串,比apply快得多。

The regex pattern above says a range of az followed by AZ .上面的正则表达式模式表示一系列az后跟AZ

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

相关问题 Python Regex - 检查大写字母后面的大写字母 - Python Regex - checking for a capital letter with a lowercase after Python:在大写字母前查找小写/数字的正则表达式条件 - Python: regex condition to find lower case/digit before capital letter 识别罗马数字后跟“.”、空格和大写字母。 (正则表达式) - Recognize roman numeral followed by '.', space and then capital letter. (RegEx) 如果大写字母前面和后面跟着一个小写字母,则插入空格 - Python - Insert space if uppercase letter is preceded and followed by one lowercase letter - Python python regex字母后必须跟另一个字母 - python regex letter must be followed by another letter 如何在Python中获取第一个大写字母,然后再获取每个不跟另一个大写字母的字母? - How to get the first capital letter and then each that isn't followed by another capital letter in Python? Python 正则表达式拆分数字和大写字母 - Python regex to split both on number and on capital letter 程序:在word中查找大写字母python - Program :find Capital letter in word in python 如何使用正则表达式从 NLTK 语料库中查找大写字母单词? - How to find a capital letter words from an NLTK corpus using regex? 匹配char的Python RegEx,后跟/以相同的char开头,但大写/小写 - Python RegEx that matches char followed/preceded by same char but uppercase/lowercase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM