正则表达式匹配特定字符串后跟6位数字。

huangapple go评论64阅读模式
英文:

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&#39;m trying to match all the strings that have either ```&quot;permit&quot;|&quot;license&quot;|&quot;license number&quot;|license num&quot;``` followed by exactly 6 digits.

I&#39;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&#39;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 = &quot;license 123456&quot;
str2 = &quot;address 123456&quot;
str3 = &quot;license number 1234567&quot;
str4 = &quot;license num 1234&quot;
str_lst=[str1,str2,str3,str4]
import re
for i in str_lst:
    try:
        print(re.search(&quot;(permit|license)[^0-9]*[0-9]{6}$&quot;, i).string)
    except AttributeError :
        print(&#39;not matching&#39;)

huangapple
  • 本文由 发表于 2023年4月6日 21:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定