用于提取数字的正则表达式 [英] Regular expression for extracting a number

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

问题描述

我希望能够从格式如下的字符串中提取一个数字:

I would like to be able to extract a number from within a string formatted as follows:

"<[1085674730]> 你好 foo1,你好吗?"

"<[1085674730]> hello foo1, how are you doing?"

我是正则表达式的新手,我只想提取一个包含在大于/小于和括号符号中的数字,但我不知道如何去做.我只需要匹配数字,但我不确定只在这些符号中搜索使用什么语法.

I'm a novice with regular expressions, I only want to be able to extract a number that is enclosed in the greater/less-than and bracket symbols, but I'm not sure how to go about it. I have to match numeric digits only, but I'm not sure what syntax is used for only searching within these symbols.

更新:

感谢大家的输入,很抱歉没有更具体,正如我向 kiamlaluno 解释的那样,我使用 VB.Net 作为我的应用程序的语言.我想知道为什么某些实现不起作用.事实上,唯一起作用的是 Matthew Flaschen 描述的那个.但这会捕获数字周围的符号以及数字本身.我只想捕获包含在符号中的数字并过滤掉符号本身.

Thank you all for you input, sorry for not being more specific, as I explained to kiamlaluno, I'm using VB.Net as the language for my application. I was wondering why some of the implementations were not working. In fact, the only one that did work was the one described by Matthew Flaschen. But that captures the symbols around the number as well as the number itself. I would like to only capture the number that is encased in the symbols and filter out the symbols themselves.

推荐答案

将 Mathews 帖子与环视相结合 http://www.regular-expressions.info/lookaround.html.这将排除前缀和后缀.

Combine Mathews post with lookarounds http://www.regular-expressions.info/lookaround.html. This will exclude the prefix and suffix.

(?<=<\[)\d+(?=\]>)

我没有测试这个正则表达式,但它应该非常接近你所需要的.仔细检查提供的链接.

I didn't test this regex but it should be very close to what you need. Double check at the link provided.

希望这有帮助!

这篇关于用于提取数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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