[英]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.