python - 非字母的正则表达式或什么都没有
问题描述
我想从 python 字符串中识别“如何”。
string1
how to find
it is a show
okay how to
用过的:
df[df['string1'].str.contains("how",case=False)]
输出来:
string1
how to find
it is a show
okay how to
因为“show”包含“how”
需要的输出:
string1
how to find
okay how to
之后我用
df[df['string1'].str.contains(r"\Whow",case=False)]
但我得到的输出是:
string1
okay how to
这又是磨损的。
正则表达式 '\W' 不包含任何内容。
如何做到这一点?
解决方案
您需要该模式的边界(\b
),否则它也会匹配单词中包含的子字符串:
df[df['string1'].str.contains(r"\bhow\b",case=False)]
string1
1 how to find
3 okay how to
dtype: object
推荐阅读
- python - 为什么从方法和外部读取的类变量不同?
- c++ - RtMidiIn 在发行版 C++ 中找不到端口
- javascript - 为什么核心装饰器的自动绑定定义了两次绑定函数?
- c# - 如何解决,UIAlertController (ActionSheet) is Crashed on iPad in xamarin.ios native app?
- ide - 在 PhpStorm 滚动错误消息预览中更改颜色
- php - TCPDF如何禁用字体斜体?
- django - Django Rest Framework 中的哪些字段允许 POST 数据结合 FileField 和 IntegerField?
- c++ - 切换 NvAPI_Stereo_Deactivate/NvAPI_Stereo_activate 会使统一应用程序崩溃
- javascript - 淘汰赛:表选择取消选择行。取消选择行的问题/挑战
- c# - MVVM - 附加属性 selectionstart,文本框中的 selectionlength