Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串中提取未指定的字符_C#_String - Fatal编程技术网

C# 从字符串中提取未指定的字符

C# 从字符串中提取未指定的字符,c#,string,C#,String,我试图找出如何从字符串中提取未指定的字符 例如:我有长字符串“1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1”等等。我想做的是在每个数字后面拆分字符串。我已经按照前面的教程使用了string.split('1'),但是我需要提取可能不同的整个数字部分。我是否可以指定类似于string.split('*..*.*')的内容,其中星号表示1-9之间的任何数字 谢谢我建议使用。我相信你想要: 至少一位数字 尽可能多地出现“一个点后至少有一个数字” 这是由的正则表达式表示

我试图找出如何从字符串中提取未指定的字符

例如:我有长字符串
“1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1”
等等。我想做的是在每个数字后面拆分字符串。我已经按照前面的教程使用了string.split('1'),但是我需要提取可能不同的整个数字部分。我是否可以指定类似于
string.split('*..*.*')
的内容,其中星号表示1-9之间的任何数字

谢谢

我建议使用。我相信你想要:

  • 至少一位数字
  • 尽可能多地出现“一个点后至少有一个数字”
这是由的正则表达式表示的

\d+(\.\d+)*
  • \d
    表示“数字”
  • +
    表示“至少一个”
  • \。
    表示“一个点”(只是
    表示“任何字符”)
  • *
    表示“零或更多”
  • 括号将“点后跟至少一个数字”分组
下面是一个完整的示例,说明了这一点:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        List<string> pieces = SplitToDottedNumbers("1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1");
        foreach (string piece in pieces)
        {
            Console.WriteLine(piece);
        }
    }

    static List<string> SplitToDottedNumbers(string text)
    {
        // Inline for readability. You could create this just once.
        var regex = new Regex(@"\d+(\.\d+)*");
        // LINQ-based implementation
        return regex.Matches(text)
            .Cast<Match>()
            .Select(match => match.Value)
            .ToList();

        /* Alternative implementation
        var pieces = new List<string>();
        var match = regex.Match(text);
        while (match.Success)
        {
            pieces.Add(match.Value);
            match = match.NextMatch();
        }
        return pieces;
        */
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
课堂测试
{
静态void Main()
{
列表项=拆分为多个数字(“1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1”);
foreach(一段一段)
{
控制台。书写线(件);
}
}
静态列表SplitToDottedNumber(字符串文本)
{
//内联的可读性。你可以创建一次。
var regex=new regex(@“\d+(\。\d+*”);
//基于LINQ的实现
返回regex.Matches(文本)
.Cast()
.Select(匹配=>match.Value)
.ToList();
/*替代实施
var pieces=新列表();
var match=regex.match(文本);
while(匹配成功)
{
件.增加(匹配值);
match=match.NextMatch();
}
返回件;
*/
}
}
(提取所有值可能有一种更简单的方法。您可以调用
Matches

,我建议使用。我相信您需要:

  • 至少一位数字
  • 尽可能多地出现“一个点后至少有一个数字”
这是由的正则表达式表示的

\d+(\.\d+)*
  • \d
    表示“数字”
  • +
    表示“至少一个”
  • \。
    表示“一个点”(只是
    表示“任何字符”)
  • *
    表示“零或更多”
  • 括号将“点后跟至少一个数字”分组
下面是一个完整的示例,说明了这一点:

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        List<string> pieces = SplitToDottedNumbers("1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1");
        foreach (string piece in pieces)
        {
            Console.WriteLine(piece);
        }
    }

    static List<string> SplitToDottedNumbers(string text)
    {
        // Inline for readability. You could create this just once.
        var regex = new Regex(@"\d+(\.\d+)*");
        // LINQ-based implementation
        return regex.Matches(text)
            .Cast<Match>()
            .Select(match => match.Value)
            .ToList();

        /* Alternative implementation
        var pieces = new List<string>();
        var match = regex.Match(text);
        while (match.Success)
        {
            pieces.Add(match.Value);
            match = match.NextMatch();
        }
        return pieces;
        */
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Text.RegularExpressions;
课堂测试
{
静态void Main()
{
列表项=拆分为多个数字(“1.1.1 sadsajkdn 1.1.2.1 dfkjskf 2.1.1”);
foreach(一段一段)
{
控制台。书写线(件);
}
}
静态列表SplitToDottedNumber(字符串文本)
{
//内联的可读性。你可以创建一次。
var regex=new regex(@“\d+(\。\d+*”);
//基于LINQ的实现
返回regex.Matches(文本)
.Cast()
.Select(匹配=>match.Value)
.ToList();
/*替代实施
var pieces=新列表();
var match=regex.match(文本);
while(匹配成功)
{
增加(匹配值);
match=match.NextMatch();
}
返回件;
*/
}
}

(提取所有值可能有一种更简单的方法。你可以调用
Matches

,我会马上写一个答案。顺便说一句,关于这个问题,ASP.NET或MVC没有什么特别的。我建议删除这些标记-当然,我的答案只是一个控制台应用程序。如果你相信这个应用程序中有ASP.NET特定的东西问题,这个问题本身就值得解释。看看常规的解释。可能的重复我会马上写一个答案。顺便说一句,关于这一点,ASP.NET或MVC没有什么特别的。我建议删除这些标记-当然,我的答案只是一个控制台应用程序。如果你相信有这样的东西P.NET-关于这个问题,值得在问题本身中解释一下。看看常规的解释。可能是真棒的重复,这正是我在搜索表单的内容。谢谢真棒,这正是我在搜索表单的内容。谢谢