简体   繁体   English

如果我只想匹配字母单词,如何在Python中使用re.compile

[英]How to use re.compile in Python if I want to match alphabetic word only

I'm learning the RE module for Python and doing some experiment. 我正在学习Python的RE模块并进行一些实验。 I have question regarding using expression, here is the example: 我对使用表达式有疑问,这是示例:

name = 'abc123def456'
m = re.compile('.*[^0-9]').match(name)
m.group()
print m

Result is 'abc123def' 结果是“ abc123def”

What should I do if I want to totally take out the numeric number 如果我想完全取出数字怎么办

Thank you! 谢谢!

You can extract all occurrences of alphabets and concatenate them to get just the alphabets in the string. 您可以提取所有出现的字母并将其连接起来,以仅获取字符串中的字母。 See below: 见下文:

"".join(re.findall("[a-zA-Z]+",name))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM