【问题标题】:How to separate numbers from words, chars and any other marks with whitespace in string如何在字符串中使用空格将数字与单词、字符和任何其他标记分开
【发布时间】:2017-03-12 13:06:24
【问题描述】:

我正在尝试将数字与单词或字符分开,并且字符串中带有空格的任何其他标点符号将它们写在一起,例如字符串是:

 string input = "ok, here is369 and777, and 20k0 10+1.any word.";

所需的输出应该是:

 ok, here is 369 and 777 , and 20 k 0 10 + 1 .any word. 

我不确定我是否走在正确的道路上,但现在我要做的是查找字符串是否包含数字,然后以某种方式将其全部替换为相同的值,但之间有空格。如果可能的话,我怎样才能找到所有单独的数字(不是数字中的每个数字都更清楚),用单词或空格分隔或不分隔,并将每个找到的数字附加到值,可以一次全部使用以替换它具有相同的数字,但两侧有空格。这样它只返回字符串中第一次出现的数字:

class Program
{
    static void Main(string[] args)
    {
        string input = "here is 369 and 777 and 15 2080 and 579"; 
        string resultString = Regex.Match(input, @"\d+").Value;

        Console.WriteLine(resultString);

        Console.ReadLine();
    }
}

输出:

369

但我也不确定是否可以为每个单个替换值获得所有不同的找到数字。最好知道该往哪个方向走

【问题讨论】:

    标签: c# string variables text replace


    【解决方案1】:

    如果我们需要的基本上是在数字周围添加空格,试试这个:

    string tmp = Regex.Replace(input, @"(?<a>[0-9])(?<b>[^0-9\s])", @"${a} ${b}");
    string res = Regex.Replace(tmp,   @"(?<a>[^0-9\s])(?<b>[0-9])", @"${a} ${b}");
    

    上一个答案假设单词、数字和标点应该分开:

    string input = "here is369 and777, and 20k0";
    var matches = Regex.Matches(input, @"([A-Za-z]+|[0-9]+|\p{P})");
    foreach (Match match in matches)
        Console.WriteLine("{0}", match.Groups[1].Value);
    

    以短方式构造所需的结果字符串:

    string res = string.Join(" ", matches.Cast<Match>().Select(m => m.Groups[1].Value));
    

    【讨论】:

    • 打印语句应该是 Console.Write("{0} ", match.Groups[1].Value);
    • @inquisitive_mind 我不想通过添加逻辑来避免结束空格使其变得更复杂。
    • @inquisitive_mind 无论如何,添加了一个基于 Linq 的 oneliner。
    • @AlexD 你好,是的好方法,但在我的案例中我遇到了两个问题,首先,它不仅将数字与其他所有内容分开,而且还将标点符号与单词分开。另一个是,因为我想将数字与所有内容分开,但同时我想将其他所有内容保存在字符串中,使用此代码,我不确定为什么,但我在输出中丢失了“+”号。
    • @MDaruwalla 要求分隔标点符号:here is 369 and 777 , and 20 k 0, 之前有一个空格。你能给出不合格的样品吗?什么是输入,什么是期望的输出。
    【解决方案2】:

    这提供了所需的输出:

    string input = "ok, here is369 and777, and 20k0 10+1.any word.";
    var matches = Regex.Matches(input, @"([\D]+|[0-9]+)");
    foreach (Match match in matches)
        Console.Write("{0} ", match.Groups[0].Value);
    

    [\D] 将匹配任何非数字。请注意 {0} 后的空格。

    【讨论】:

    • 您好,似乎与上面的答案相同,这是一个很好的解决方案,但不适用于我的情况,因为它不仅将数字与其他所有内容分开,还将标点符号与单词分开。因为我想要将数字与所有内容分开,但同时我希望将其他所有内容保留在字符串中,但由于某种原因,我在输出中丢失了此代码的“+”号。
    【解决方案3】:

    你走在正确的道路上。 Regex.Match 仅返回一个匹配项,您必须使用 .NextMatch() 来获取与您的正则表达式匹配的下一个值。 Regex.Matches 将所有可能的匹配项返回到 MatchCollection 中,然后您可以像我在示例中所做的那样使用循环进行解析:

    string input = "here is 369 and 777 and 15 2080 and 579";
    
            foreach (Match match in Regex.Matches(input, @"\d+"))
            {
                Console.WriteLine(match.Value);
            }
    
            Console.ReadLine();
    

    这个输出:

    369
    777
    15
    2080
    579
    

    【讨论】:

    • 你好,是的,似乎我也能以某种方式弄清楚,但上面的解决方案完全回答了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多