如何在C#中使用正则表达式获取某个特定单词之前的数字? [英] How to get the digits before some particular word using regex in c#?

查看:27
本文介绍了如何在C#中使用正则表达式获取某个特定单词之前的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将使用下面的正则表达式来获取单词之前的数字.

We will use below regex to get the digits before the words.

示例:

838123 someWord 8 someWord 12 someWord

838123 someWord 8 someWord 12 someWord

(\ d +)\ s * someWord

但是有时数字和单词之间会出现任何东西.请参见下面的示例行.

But sometimes anything will come between Number and word.Please see the below example line.

例如:

某字词12的43434任意字词2323新的字词

43434 of someword 12 anything someword 2323 new someword

如何使用正则表达式获取该单词之前的确切数字?

How to get the exact digit before that word using regex?

请给我您的建议.

推荐答案

执行此操作:

(\ d +)[^ \ d] + some [wW] ord

除了数字本身,您需要接受其他任何内容.我还考虑了 w W ,因为您的示例包含了两者.

You need to accept anything other than digits themselves. Also I considered both w and W since your examples contained both.

演示

这篇关于如何在C#中使用正则表达式获取某个特定单词之前的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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