regex - Detecting Special Characters with Regular Expression in python?
问题描述
df
Name
0 @#
1 R@#
2 ghj@#
3 Ray
4 *@+
5 Jack
6 Sara123#
7 ( 1234. )
8 Benjamin k 123
9 _
10 _!@#_
11 _#_&@+-
12 56#@!
Output:
Bad_Name
0 @#
1 *@+
2 _
3 _!@#_
4 _#_&@+-
I need to detect the special character through regular expression. If a string contains any alphabet or Number then that string is valid else it will consider as bad string. I was using '^\W*$' RE, everything was working fine except when the string contains '_'( underscore) it is not treating as Bad String.
解决方案
Use pandas.Series.str.contains
:
df[~df['Name'].str.contains('[a-z0-9]', False)]
Output:
Name
0 @#
4 *@+
9 _
10 _!@#_
11 _#_&@+-
推荐阅读
- ios - 删除项目 RxSwift MVVM 模式
- angular - 使用 cdk Angular 材料将卡片从容器移动到相应的容器
- typescript - 如何覆盖外部 TypeScript 接口
- redis - Redis 内存使用和信息内存
- python - 当 x 范围为 0、0.001、0.01、...、1000 时,如何手动拆分 matplotlib 散点图的 x 轴?
- haskell - 如果恢复容器很困难,“Store”comonad 的意义何在?
- c# - Drop 事件在继承的面板上触发两次
- python - 如何使用moviepy动画正确引用无花果和斧头
- c++ - 类大小对成员函数性能的影响
- ios - 使用全屏图像和导航查看链接