正则表达式-查找字符串中的特定数字 [英] Regex - find specific number in string

查看:430
本文介绍了正则表达式-查找字符串中的特定数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在正则表达式方面苦苦挣扎,找不到解决方案. 我正在尝试在包含数字,分号,冒号和空格的字符串中找到一些特定的数字.

I'm struggling with regular expression whole day and couldn't find a solution. I'm trying to find some specific number in strings that contains numbers, semicolons, colons and whitespaces.

就我们的目的而言,假设我正在寻找电话号码 1234

For our purpose let's say I'm looking for number 1234

以下是一些应该匹配的示例(每行都是不同的字符串):

Here are few examples which should match (Every line is a different string):

1234
;1234;
1234 : 5678
;1234,3321

和不匹配的示例(因为它是不同的数字):

And example that shouldn't match (because it's different number):

;12345;
0123456

我当前的尝试:

[^(0-9*)]1234[^(0-9*)]

这里是我的问题与Regex Tester的永久链接:

Here is a permalink to Regex Tester with my problem: Regex Tester fiddle

推荐答案

也许可以尝试以下方法:([^0-9]|^)1234([^0-9]|$)在这种情况下,您不需要环视功能.

Maybe try this: ([^0-9]|^)1234([^0-9]|$) In this case you don't need the lookaround features.

您可以使用它更好地了解regexp.它具有很好的gui可视化模式. Debuggex

You can use this to understand regexp better. It has a nice gui to visualize the pattern. Debuggex

这篇关于正则表达式-查找字符串中的特定数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆