用于匹配德国邮政编码的正则表达式,但不是
以下字符串:
23434 5465434
58495 / 46949345
58495 - 46949345
58495 / 55643
d 44444 ssdfsdf
64784
45643dfgh
58495/55643
48593/48309596
675643235
34565435 34545
它只想要提取粗体。它是一个五位数字(德语)。 它不应与电话号码 43564 366334
或 45433 / 45663
等匹配,如我上面的示例所示。
我尝试了类似 ^\b\d{5}
的东西,但这不是一个好的开始。
有一些提示可以让我正常工作吗?
感谢所有提示
following string:
23434 5465434
58495 / 46949345
58495 - 46949345
58495 / 55643
d 44444 ssdfsdf
64784
45643 dfgh
58495/55643
48593/48309596
675643235
34565435 34545
it only want to extract the bold ones. its a five digit number(german).
it should not match telephone numbers 43564 366334
or 45433 / 45663
,etc as in my example above.
i tried something like ^\b\d{5}
but thats not a good beginning.
some hints for me to get this working?
thanks for all hints
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以添加否定的前瞻断言以避免与电话号码匹配。
如果您使用的是 Ruby 1.9,则还可以添加否定的后视断言。
You could add a negative look-ahead assertion to avoid the matches with phone numbers.
If you're using Ruby 1.9, you can add a negative look-behind assertion as well.
您尚未指定如何区分您要搜索的号码。
根据您提供的示例字符串,您似乎只想:
^(\d{5})\n
匹配以 5 位数字开头且不包含任何其他内容的行。
您可能希望在前 5 位数字后允许有一些空格(但不允许有其他空格):
^(\d{5})\s*\n
You haven't specified what distinguishes the number you're trying to search for.
Based on the example string you gave, it looks like you just want:
^(\d{5})\n
Which matches lines that start with 5 digits and contain nothing else.
You might want to permit some spaces after the first 5 digits (but nothing else):
^(\d{5})\s*\n
我不完全确定指定的规则。但是,如果您想要以 5 位数字开头且不包含其他数字的行,这可能会起作用:
如果前导空格没问题,那么:
这是 显示结果的圆形链接。
I'm not completely sure about the specified rules. But if you want lines that start with 5 digits and do not contain additional digits, this may work:
If leading white space is okay, then:
Here is the Rubular link that shows the result.
这应该(未经测试)匹配:
非数字行结尾
数字(或一些非数字,然后是五位数字)
\1 将是五位数字
\2 将是整个后半部分,如果有的话
\3 将是数字后面的单词,如果有任何
编辑以适合提问者编辑的问题
编辑再次:我想出了一个更优雅的解决方案:
This should (it's untested) match:
a space, and ending with some non-numbers
digits (or some non-digits and then five digits)
\1 would be the five digits
\2 would be the whole second half, if any
\3 would be the word after the digits, if any
edited to fit the asker's edited question
edit again: I came up with a much more elegant solution: