简体   繁体   English

如何在字符串中使用空格将数字与单词,字符和任何其他标记分开

[英]How to separate numbers from words, chars and any other marks with whitespace in string

I'm trying to separate numbers from words or characters and any other punctuation with whitespace in string wrote them together eg string is: 我正在尝试将数字与单词或字符以及字符串中带有空格的任何其他标点符号分开写入它们,例如,字符串为:

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

and desired output should be: 所需的输出应为:

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

I'm not sure if I'm on right way, but now what I'm trying to do, is to find if string contains numbers and then somehow replace it all with same values but with whitespace between. 我不确定我的方法是否正确,但是现在我要尝试的是查找字符串是否包含数字,然后以某种方式将其全部替换为相同的值,但之间使用空格。 If it is possible, how can I find all individual numbers (not each digit in number to be clearer), separated or not separated by words or whitespace and attach each found number to value, which can be used for all at once to replace it with same numbers but with spaces on sides. 如果有可能,我如何找到所有单独的数字(不是每个数字都更清晰),用单词或空格分隔或不分隔,并将每个找到的数字附加到值上,这些值可一次全部用于替换具有相同的数字,但侧面带有空格。 This way it returns only first occurrence of a number in string: 这样,它仅返回字符串中数字的首次出现:

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();
    }
}

output: 输出:

369

but also I'm not sure if I can get all different found number for single replacement value for each. 但是我也不确定是否可以为每个替换值获得所有不同的发现编号。 Would be good to find out in which direction to go 最好找出去向

If what we need is basically to add spaces around numbers, try this: 如果我们基本上需要在数字周围添加空格,请尝试以下操作:

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}");

Previous answer assumed that words, numbers and punctuation should be separated: 先前的答案假定单词,数字和标点符号应分开:

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);

To construct the required result string in a short way: 简短地构造所需的结果字符串:

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

You were on the right path. 您走在正确的道路上。 Regex.Match only returns one match and you would have to use .NextMatch() to get the next value that matches your regular expression. Regex.Match仅返回一个匹配项,您必须使用.NextMatch()来获取与您的正则表达式匹配的下一个值。 Regex.Matches returns every possible match into a MatchCollection that you can then parse with a loop as I did in my example: Regex.Matches将所有可能的匹配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();

This Outputs: 输出:

369
777
15
2080
579

This provides the desired output: 这提供了所需的输出:

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] will match anything non digit. [\\ D]将匹配非数字的任何内容。 Please note space after {0}. 请注意{0}后的空格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将String分成chars - How to separate String into chars 如何在不复制逗号或任何其他标点符号的情况下将文本分隔为单词? - How to separate text to words without to copy comma or any other punctuation? 我当前的问题是我想将字符串分成单个单词和标点符号,但不知道如何 - My current problem is that I want to separate a string into single words and punctuation marks but don't know how 如何从列表中删除空格,不需要的字符以比较字符串值C# - How to remove whitespace, unwanted chars from list to compare string values c# 如何在 c# 中的空格后分隔字符串 - How to separate string after whitespace in c# 从字符串获取数字并粘上字符 - Getting a numbers from a string with chars glued 如何替换字符串中除几个字符外的所有字符和数字 - How to I replace all chars and numbers in string except several chars 我有正则表达式将字符串拆分为单词,数字和标点符号列表。 如何制作列表中的“az”和“0-9”单个元素? - I have regex to split string to words, numbers and punctuation marks list. How to make “a-z” and “0-9” single elements of list? 如何添加空格以使用标点符号分隔合并的单词(如果它位于中间并用空格保留) - how to add whitespace to separate merged words with punctuation mark if it is between and keep it with whitespace 如何正确地将字符串转换为单词和标点符号列表? - How can I properly turn string to list of words and punctuation marks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM