初学者:REGEX 匹配数字序列,但单词“CODE”除外。存在于一条线上

发布于 2024-08-15 19:52:59 字数 787 浏览 2 评论 0原文

我已经在很长一段时间里摸索着正则表达式了,但可惜的是,我无法帮助有需要的朋友。

我的“朋友”正在尝试匹配文本文件中符合以下条件的所有行:

  1. 只有 7 到 10 位数字(0123456 或 0123456789)
  2. 只有 7 到 10 位数字,然后是破折号,然后是另外两位数字(0123456) -01 或 0123456789-01)
  3. 匹配以上任何内容除了,其中 Code/code 或 Passcode/passcode 一词位于要匹配的数字之前(例如“访问代码: 16434629”或“密码 5253443-12”)
  4. 编辑:只需要匹配的数字,不需要其他。

这是我见过的最讨厌的正则表达式,“他”给了我:

^(?=.*?[^=/%:]\b\d{7,10}((\d?\d?)|(-\d\d))?\b)((?!Passcode|passcode|Code|code).)*$

...

问题:有没有办法使用短正则表达式来查找满足上述条件的所有行?

假设PCRE。我的朋友提前感谢你。 ;-)

顺便说一句 - 我无法找到 stackoverflow.com 或 superuser.com 中列出的任何其他问题可以准确回答这个问题。

编辑:我正在使用 Kodos Python Regex Debugger 来验证和测试正则表达式。

I've been able to stumble my way through regular expressions for quite some time, but alas, I cannot help a friend in need.

My "friend" is trying to match all lines in a text file that match the following criteria:

  1. Only a 7 to 10 digit number (0123456 or 0123456789)
  2. Only a 7 to 10 digit number, then a dash, then another two digits (0123456-01 or 0123456789-01)
  3. Match any of the above except where the words Code/code or Passcode/passcode is before the numbers to match (Such as "Access code: 16434629" or "Passcode 5253443-12")
  4. EDIT: Only need the numbers that match, nothing else.

Here is the nastiest regex I have ever seen that "he" gave me:

^(?=.*?[^=/%:]\b\d{7,10}((\d?\d?)|(-\d\d))?\b)((?!Passcode|passcode|Code|code).)*$

...

Question: Is there a way to use a short regex to find all lines that meet the above criteria?

Assume PCRE. My friend thanks you in advance. ;-)

BTW - I have not been able to find any other questions listed in stackoverflow.com or superuser.com which can answer this question accurately.

EDIT: I'm using Kodos Python Regex Debugger to validate and test the regex.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-08-22 19:52:59
(?<!(?:[Pp]asscode|[Cc]ode).*)[0-9]{7,10}(?:-[0-9]{2})?

评论版本:

(?<!                 # Begin zero-width negative lookbehind. (Makes sure the following pattern can't match before this position)
(?:                  # Begin non-matching group
[Pp]asscode          # Either Passcode or passcode
|                    # OR
[Cc]ode              # Either Code or code
)                    # End non-matching group
.*                   # Any characters
)                    # End lookbehind
[0-9]{7,10}          # 7 to 10 digits
(?:                  # Begin non-matching group
-[0-9]{2}            # dash followed by 2 digits
)                    # End non-matching group
?                    # Make last group optional

编辑:评论讨论后的最终版本 -

/^(?!\D*(?:[Pp]asscode|[Cc]ode))\D*([0-9]{7,10}(?:-[0-9]{2})?)/

(第一个捕获缓冲区中的结果)

(?<!(?:[Pp]asscode|[Cc]ode).*)[0-9]{7,10}(?:-[0-9]{2})?

Commented version:

(?<!                 # Begin zero-width negative lookbehind. (Makes sure the following pattern can't match before this position)
(?:                  # Begin non-matching group
[Pp]asscode          # Either Passcode or passcode
|                    # OR
[Cc]ode              # Either Code or code
)                    # End non-matching group
.*                   # Any characters
)                    # End lookbehind
[0-9]{7,10}          # 7 to 10 digits
(?:                  # Begin non-matching group
-[0-9]{2}            # dash followed by 2 digits
)                    # End non-matching group
?                    # Make last group optional

Edit: final version after comment discussion -

/^(?!\D*(?:[Pp]asscode|[Cc]ode))\D*([0-9]{7,10}(?:-[0-9]{2})?)/

(result in first capture buffer)

零時差 2024-08-22 19:52:59

您可以使用令人讨厌的正则表达式,您必须寻求帮助......

或者您可以使用两个简单正则表达式。一种可以满足您想要的内容,另一种可以过滤您不想要的内容。更简单、更具可读性。

您想读哪一篇?

$foo =~ /(?<!(?:[Pp]asscode|[Cc]ode).*)[0-9]{7,10}(?:-[0-9]{2})?/

$foo =~ /\d{7,10}(-\d{2})?/ and $foo !~ /(access |pass)code/i;

编辑:不区分大小写。

You can get by with a nasty regex you have to get help with ...

... or you can use two simple regexes. One that matches what you want, and one that filters what you don't want. Simpler and more readable.

Which one would you like to read?

$foo =~ /(?<!(?:[Pp]asscode|[Cc]ode).*)[0-9]{7,10}(?:-[0-9]{2})?/

or

$foo =~ /\d{7,10}(-\d{2})?/ and $foo !~ /(access |pass)code/i;

Edit: case-insensitivity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文