van*_*van 294 .net c# regex string
我需要查找并提取字符串中包含的数字.
例如,从这些字符串:
string test = "1 test"
string test1 = " 1 test"
string test2 = "test 99"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
Tim*_*ker 512
\d+
是整数的正则表达式.所以
//System.Text.RegularExpressions.Regex
resultString = Regex.Match(subjectString, @"\d+").Value;
Run Code Online (Sandbox Code Playgroud)
返回包含第一次出现的数字的字符串subjectString
.
Int32.Parse(resultString)
然后会给你这个号码.
Dav*_*ave 162
以下是我如何清理电话号码以获取数字:
string numericPhone = new String(phone.Where(Char.IsDigit).ToArray());
Run Code Online (Sandbox Code Playgroud)
Sas*_*nyi 53
通过字符串并使用 Char.IsDigit
string a = "str123";
string b = string.Empty;
int val;
for (int i=0; i< a.Length; i++)
{
if (Char.IsDigit(a[i]))
b += a[i];
}
if (b.Length>0)
val = int.Parse(b);
Run Code Online (Sandbox Code Playgroud)
Pra*_*ana 38
使用正则表达式...
Regex re = new Regex(@"\d+");
Match m = re.Match("test 66");
if (m.Success)
{
Console.WriteLine(string.Format("RegEx found " + m.Value + " at position " + m.Index.ToString()));
}
else
{
Console.WriteLine("You didn't enter a string containing a number!");
}
Run Code Online (Sandbox Code Playgroud)
ejc*_*tes 32
我用什么来获取没有任何标点符号的电话号码...
var phone = "(787) 763-6511";
string.Join("", phone.ToCharArray().Where(Char.IsDigit));
// result: 7877636511
Run Code Online (Sandbox Code Playgroud)
Tab*_*res 17
Regex.Split可以从字符串中提取数字.您将获得在字符串中找到的所有数字.
string input = "There are 4 numbers in this string: 40, 30, and 10.";
// Split on one or more non-digit characters.
string[] numbers = Regex.Split(input, @"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int i = int.Parse(value);
Console.WriteLine("Number: {0}", i);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
编号:4编号:40编号:30编号:10
spa*_*jce 15
这是一个Linq
版本:
string s = "123iuow45ss";
var getNumbers = (from t in s
where char.IsDigit(t)
select t).ToArray();
Console.WriteLine(new string(getNumbers));
Run Code Online (Sandbox Code Playgroud)
Blo*_*mer 13
这是另一个简单的解决方案,Linq
它仅从字符串中提取数值。
var numbers = string.Concat(stringInput.Where(char.IsNumber));
Run Code Online (Sandbox Code Playgroud)
例子:
var numbers = string.Concat("(787) 763-6511".Where(char.IsNumber));
Run Code Online (Sandbox Code Playgroud)
给出:“7877636511”
Bvd*_*Ven 12
你也可以试试这个
string.Join(null,System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));
Run Code Online (Sandbox Code Playgroud)
小智 12
使用Regex的另一个简单解决方案您应该使用它
using System.Text.RegularExpressions;
Run Code Online (Sandbox Code Playgroud)
而代码是
string var = "Hello3453232wor705Ld";
string mystr = Regex.Replace(var, @"\d", "");
string mynumber = Regex.Replace(var, @"\D", "");
Console.WriteLine(mystr);
Console.WriteLine(mynumber);
Run Code Online (Sandbox Code Playgroud)
Dan*_*ger 11
只需使用RegEx匹配字符串,然后转换:
Match match = Regex.Match(test , @"(\d+)");
if (match.Success) {
return int.Parse(match.Groups[1].Value);
}
Run Code Online (Sandbox Code Playgroud)
Tar*_*lah 10
如果数字有小数点,您可以在下面使用
using System;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine(Regex.Match("anything 876.8 anything", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("anything 876 anything", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("$876435", @"\d+\.*\d*").Value);
Console.WriteLine(Regex.Match("$876.435", @"\d+\.*\d*").Value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果 :
“任何东西 876.8 任何东西”==> 876.8
“任何东西 876 任何东西”==> 876
“$876435”==> 876435
“$876.435”==> 876.435
示例:https : //dotnetfiddle.net/IrtqVt
对于那些想要在两行中使用Regex的字符串中的十进制数的人:
decimal result = 0;
decimal.TryParse(Regex.Match(s, @"\d+").Value, out result);
Run Code Online (Sandbox Code Playgroud)
同样的事情适用于浮动,长期等...
这是另Linq
一种从字符串中提取第一个数字的方法.
string input = "123 foo 456";
int result = 0;
bool success = int.TryParse(new string(input
.SkipWhile(x => !char.IsDigit(x))
.TakeWhile(x => char.IsDigit(x))
.ToArray()), out result);
Run Code Online (Sandbox Code Playgroud)
例子:
string input = "123 foo 456"; // 123
string input = "foo 456"; // 456
string input = "123 foo"; // 123
Run Code Online (Sandbox Code Playgroud)
您可以使用String
以下属性执行此操作:
return new String(input.Where(Char.IsDigit).ToArray());
Run Code Online (Sandbox Code Playgroud)
它只提供字符串中的数字.
var match=Regex.Match(@"a99b",@"\d+");
if(match.Success)
{
int val;
if(int.TryParse(match.Value,out val))
{
//val is set
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题没有明确说明你只想要字符0到9,但是从你的示例集和注释中认为这是不正确的.所以这是执行该操作的代码.
string digitsOnly = String.Empty;
foreach (char c in s)
{
// Do not use IsDigit as it will include more than the characters 0 through to 9
if (c >= '0' && c <= '9') digitsOnly += c;
}
Run Code Online (Sandbox Code Playgroud)
为什么你不想使用Char.IsDigit() - 数字包括分数,下标,上标,罗马数字,货币分子,环绕数字和脚本特定数字等字符.
小智 7
string input = "Hello 20, I am 30 and he is 40";
var numbers = Regex.Matches(input, @"\d+").OfType<Match>().Select(m => int.Parse(m.Value)).ToArray();
Run Code Online (Sandbox Code Playgroud)
var outputString = String.Join("", inputString.Where(Char.IsDigit));
Run Code Online (Sandbox Code Playgroud)
获取字符串中的所有数字.因此,如果您使用examaple'1 plus 2',它将获得'12'.
扩展方法来获取字符串中包含的所有正数:
public static List<long> Numbers(this string str)
{
var nums = new List<long>();
var start = -1;
for (int i = 0; i < str.Length; i++)
{
if (start < 0 && Char.IsDigit(str[i]))
{
start = i;
}
else if (start >= 0 && !Char.IsDigit(str[i]))
{
nums.Add(long.Parse(str.Substring(start, i - start)));
start = -1;
}
}
if (start >= 0)
nums.Add(long.Parse(str.Substring(start, str.Length - start)));
return nums;
}
Run Code Online (Sandbox Code Playgroud)
如果还需要负数,只需修改此代码即可处理减号(-
)
鉴于此输入:
"I was born in 1989, 27 years ago from now (2016)"
Run Code Online (Sandbox Code Playgroud)
结果数字列表将是:
[1989, 27, 2016]
Run Code Online (Sandbox Code Playgroud)
Ahmad Mageed在这里提供了一种有趣的方法,使用正则表达式并StringBuilder
按照整数在字符串中出现的顺序提取整数。
基于 Ahmad Mageed 帖子的使用示例Regex.Split
如下:
var dateText = "MARCH-14-Tue";
string splitPattern = @"[^\d]";
string[] result = Regex.Split(dateText, splitPattern);
var finalresult = string.Join("", result.Where(e => !String.IsNullOrEmpty(e)));
int DayDateInt = 0;
int.TryParse(finalresult, out DayDateInt);
Run Code Online (Sandbox Code Playgroud)
我已经使用这一行从任何字符串中提取所有数字。
var phoneNumber = "(555)123-4567";
var numsOnly = string.Join("", new Regex("[0-9]").Matches(phoneNumber)); // 5551234567
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
490376 次 |
最近记录: |