搜索给定字符串中的数字. [英] Searching for numbers in a given string.

查看:78
本文介绍了搜索给定字符串中的数字.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可能包含数字的字符串.我想编写一个搜索包含数字的子字符串的函数.

我无法搜索字符串中的数字

请向我提供任何有帮助的想法.

I have a string which may contain numbers.I want to write function which searches for substring which contains numbers.

I am not able to search for numbers in string

Please provide me with any ideas which will help.

推荐答案

尝试这个正则表达式字符串的男孩:\b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b

它只会找到包含数字的字符串.

这样的字符串"Dam It12不是45 g00d".

会给你:

"It12"
"not45"
"g00d"

例子
Try this bade boy of a regex string: \b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b

It will find only strings that contains numbers.

string like this "Dam It12 is not45 that g00d".

Will give you:

"It12"
"not45"
"g00d"

Example
System.Text.RegularExpressions.MatchCollection ms = System.Text.RegularExpressions.Regex.Matches("Dam It12 is not45 that g00d", @"\b(?=[-/\\\\A-Za-z]*[0-9])(?=[-/\\\\0-9]*[A-Za-z])[A-Za-z0-9]+(?:[-/\\\\][A-Za-z0-9]+)*\b");

foreach (System.Text.RegularExpressions.Match m in ms)
{
  Console.WriteLine(m.ToString());
}


  string abc = "deepthi12 "
char[] SpecialChars = "0123456789";.ToCharArray();
int indexOf = abc.IndexOfAny(SpecialChars);
if (indexOf != -1)
{
    //there should a number in the string
}
else
{
    //No number
}


您可以使用Regex.Replace()进行此操作.
例如Regex.Replace(myString, @"\d", string.empty);
You can use Regex.Replace() to do this.
e.g. Regex.Replace(myString, @"\d", string.empty);


这篇关于搜索给定字符串中的数字.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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