C# 读取具有特定格式的字符串
我有一些字符串,比如C# 读取具有特定格式的字符串,c#,C#,我有一些字符串,比如 string text = "these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7." 所以这里我只想读000\u 00\u 0,649\u 17\u 8,975\u 64\u 7。。。使用此格式的所有字符串 请帮助我了解情况您可以使用Regex类及其匹配方法 var matches = S
string text =
"these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7."
所以这里我只想读000\u 00\u 0
,649\u 17\u 8
,975\u 64\u 7
。。。使用此格式的所有字符串
请帮助我了解情况您可以使用
Regex
类及其匹配方法
var matches = System.Text.RegularExpressions.Regex.Matches(input, "([_0-9]+)");
var numberishs1 = matches
.Select(m => m.Groups[1].ToString())
.ToList();
var s1 = string.Join(", ", numberishs1);
你必须使用正则表达式 在您的例子中,您希望找到模式
“…。。。。。
如果你只想要数字,它将是模式“([0-9]{3}{[0-9]{2}{[0-9]{1})”
试试这个:
using System;
using System.Text.RegularExpressions;
namespace SyntaxTest
{
class Program
{
static void Main(string[] args)
{
var input =
"these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7.";
var pattern = "..._.._.";
Match result = Regex.Match(input, pattern);
if (result.Success)
{
while (result.Success)
{
Console.WriteLine("Match: {0}", result.Value);
result = result.NextMatch();
}
Console.ReadLine();
}
}
}
}
似乎是正则表达式的工作…模式:([0-9]{3}{[0-9]{2}{[0-9]{1})可能
[0-9]+([0-9]+)*
将是一种更安全的模式,因为[\u0-9]+
无论何时出现都匹配单个
,例如在中“仅某些文本”
using System;
using System.Text.RegularExpressions;
namespace SyntaxTest
{
class Program
{
static void Main(string[] args)
{
var input =
"these all are strings, 000_00_0 and more strings are there I have here 649_17_8, and more with this format 975_63_7.";
var pattern = "..._.._.";
Match result = Regex.Match(input, pattern);
if (result.Success)
{
while (result.Success)
{
Console.WriteLine("Match: {0}", result.Value);
result = result.NextMatch();
}
Console.ReadLine();
}
}
}
}