gpt4 book ai didi

regex - str.startswith 使用正则表达式

转载 作者:行者123 更新时间:2023-12-01 22:35:04 30 4
gpt4 key购买 nike

我能理解为什么 str.startswith() 不处理正则表达式吗:

   col1
0 country
1 Country

i.e : df.col1.str.startswith('(C|c)ountry')

它返回所有值 False :

   col1
0 False
1 False

最佳答案

Series.str.startswith 不接受正则表达式,因为它的行为类似于普通 Python 中的 str.startswith,后者不接受正则表达式。另一种方法是使用正则表达式匹配(如解释 in the docs ):

df.col1.str.contains('^[Cc]ountry')

字符类 [Cc] 可能是比 (C|c)< 更好的匹配 Cc 的方法,当然除非您需要捕获使用了哪个字母。在这种情况下,您可以执行([Cc])

关于regex - str.startswith 使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39129020/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com