简体   繁体   English

如何使用Python Regex忽略字符

[英]How to ignore characters with Python Regex

I am wondering if there is a better Python Regex solution for the one that I currently have? 我想知道我现在有没有更好的Python Regex解决方案? Currently my code is: 目前,我的代码是:

import re

n = '+17021234567'
m = '7021234567'
match = re.search(r'(?:\+1)?(\d{10})', n)
match.group(1)

match = re.search(r'(?:\+1)?(\d{10})', m)
match.group(1)

The goal of the code is to only extract the 10 digit ph # if it has a leading +1 or not. 该代码的目标是仅提取10位ph#(如果其前导+1与否)。 Currently it works, but I am wondering is there a way to just call match.group() to get the 10 digit ph # without calling match.group(1) ? 目前它有效,但是我想知道是否有一种方法可以只调用match.group()来获取10位ph#而不调用match.group(1)吗?

No, without the use of capturing groups, it couldn't be possible through re.match function. 不,如果不使用捕获组,则无法通过re.match函数来实现。 Since re.match tries to match the input string from the beginning. 由于re.match会尝试从头开始匹配输入字符串。 But it could be possible through re.search 但它可以通过可能re.search

>>> re.search(r'\d{10}$', n).group()
'7021234567'
>>> re.search(r'\d{10}$', m).group()
'7021234567'

you want to only capture digit use '\\d' for digit 您只想捕获数字,请对数字使用“ \\ d”

n = '+17021234567'
re.findall('\d{10}$',n)

use this pattern 使用这种模式

(?<=^|\+1)\d{10}$

Demo 演示版

 (?<= look behind to see if there is: ^ the beginning of the string | OR \\+1 '+1' ) end of look-behind \\d{10} digits (0-9) (10 times) $ before an optional \\n, and the end of the string 

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

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