英文:
Regex to match specific strings followed by 6 digits
问题
I have translated the code-related part for you:
str1 = "许可证 123456"
str2 = "地址 123456"
str3 = "许可证号码 1234567"
str4 = "许可证号 1234"
我尝试匹配所有包含```许可证```、```地址```、```许可证号码```、```许可证号```之一,后跟精确6位数字的字符串。
我尝试了这个正则表达式:
```python
re.search(r"(许可证|地址|许可证号码|许可证号)\d{6}$", str)
从我的理解来看,它应该匹配我的条件,但显然也匹配了超过6位数字的字符串(它不匹配少于6位数字的字符串)。
我该如何修正我的正则表达式?
<details>
<summary>英文:</summary>
Let say I have those 4 strings:
str1 = "license 123456"
str2 = "address 123456"
str3 = "license number 1234567"
str4 = "license num 1234"
I'm trying to match all the strings that have either ```"permit"|"license"|"license number"|license num"``` followed by exactly 6 digits.
I've tried this expression :
re.search(r"(permit|license|license number|license num)\d{6}$", str)
From what I understood, it was suppose to match my criteria, but it apparently also matches string with more than 6 digits (it doesn't match strings with less than 6).
How could I fix my regex expression ?
</details>
# 答案1
**得分**: 1
我认为这可能有效:
^(permit|license|license number|license num)\s\d{6}$
或者这个,取决于你的搜索方式:
\b(permit|license|license number|license num)\s\d{6}\b
<details>
<summary>英文:</summary>
I think this may work:
^(permit|license|license number|license num)\s\d{6}$
or this, depends on how you search:
\b(permit|license|license number|license num)\s\d{6}\b
</details>
# 答案2
**得分**: 1
Here's the translated code:
首先,“license”|“license number”|“license num”是逻辑上的冗余,可以缩减为“license”本身。
其次,您没有指定可以出现在我们的许可证和数字之间的字符模式。这可以通过 .* 完成,所以主要模式变成了“permit|license.*[0-9]{6}$”。
因此,最终的代码如下:
```python
str1 = "license 123456"
str2 = "address 123456"
str3 = "license number 1234567"
str4 = "license num 1234"
str_lst=[str1,str2,str3,str4]
import re
for i in str_lst:
try:
print(re.search("(permit|license)[^0-9]*[0-9]{6}$", i).string)
except AttributeError:
print('not matching')
英文:
firstly "license"|"license number"|license num" is a logic redundancy and can be shrunk to "license" itself.
secondly you are not specifying the pattern of characters that can be present between our license and numbers. this can be done by .*
so the main pattern becomes "permit|license.*[0-9]{6}$"
so the end code can be seen as below
str1 = "license 123456"
str2 = "address 123456"
str3 = "license number 1234567"
str4 = "license num 1234"
str_lst=[str1,str2,str3,str4]
import re
for i in str_lst:
try:
print(re.search("(permit|license)[^0-9]*[0-9]{6}$", i).string)
except AttributeError :
print('not matching')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论