正则表达式模式,以防止两个相似的字符相邻

2024-05-28 18:43:31 发布

您现在位置:Python中文网/ 问答频道 /正文

Vanity Formats [img]
To make matters worse, vain fleshlings will sometimes represent phone numbers with words. These so-called vanity phone numbers start with 1 and the area code (ie. 1-123-) but after that they will continue with a mix of letters (upper or lowercase), numbers and dashes. They are restricted to using exactly 7 letters and numbers (following the 1 and area code), but dashes can be inserted at any point other than immediately after another dash. Flahlings make things so complicated!

我一直在解决这个难题;我已经能够处理第一组整数和破折号,但我不能让字母数字部分工作。 我使用的是Python3,到目前为止我已经做到了:

vanity_number = r'[+]?(1-)[0-9]{3}-[a-zA-Z0-9-]{7}'

这在一定程度上是可行的,但是我需要它来分别计算破折号和字母数字,这样像1-345-qpG-8s-vd这样的东西仍然适合这个模式。谢谢你的帮助。你知道吗


Tags: andthemakesowithphonecodearea
1条回答
网友
1楼 · 发布于 2024-05-28 18:43:31

最后一部分应该由7个字母组成。如果我理解正确的话,每个破折号后面都可以有一个可选的破折号,但是不能有两个破折号相邻,可能不应该以破折号结尾。你知道吗

^\+?1-\d{3}-(?:[A-Za-z\d]-?){7}\b$

如果允许以破折号结尾,则删除结尾处的单词边界\b。你知道吗

Here is a demo at regex101

相关问题 更多 >

    热门问题