发现在一个特定的格式事件再度发生串数发生在给定的文本 [英] Finding the number of occurences strings in a specific format occur in a given text
问题描述
我有一个大的字符串,其中可以有特定的单词(文字后跟一个冒号,如测试)发生一次以上。例如,像这样的:
I have a large string, where there can be specific words (text followed by a single colon, like "test:") occurring more than once. For example, like this:
word:
TEST:
word:
TEST:
TEST: // random text
字出现两次并且TEST发生了三次,但量可以是可变的。另外,这些词不必在相同的顺序,可以有更多的文本中的同一行的字(如图中的TEST的最后一个例子)。我需要做的就是追加的出现次数为每个单词,例如输出字符串需要是这样的:
"word" occurs twice and "TEST" occurs thrice, but the amount can be variable. Also, these words don't have to be in the same order and there can be more text in the same line as the word (as shown in the last example of "TEST"). What I need to do is append the occurrence number to each word, for example the output string needs to be this:
word_ONE:
TEST_ONE:
word_TWO:
TEST_TWO:
TEST_THREE: // random text
正则表达式获取这些话我已经写的 ^ \ B [A-ZA-Z0-9 _] {4} \ B:
。但是,我不知道如何完成上述以快速的方式。任何想法?
The RegEx for getting these words which I've written is ^\b[A-Za-z0-9_]{4,}\b:
. However, I don't know how to accomplish the above in a fast way. Any ideas?
推荐答案
正则表达式是最适合这个工作 - 通过替换一个匹配评价:
Regex is perfect for this job - using Replace with a match evaluator:
这个例子不是测试或编译:
This example is not tested nor compiled:
public class Fix
{
public static String Execute(string largeText)
{
return Regex.Replace(largeText, "^(\w{4,}):", new Fix().Evaluator);
}
private Dictionary<String, int> counters = new Dictionary<String, int>();
private static String[] numbers = {"ONE", "TWO", "THREE",...};
public String Evaluator(Match m)
{
String word = m.Groups[1].Value;
int count;
if (!counters.TryGetValue(word, out count))
count = 0;
count++;
counters[word] = count;
return word + "_" + numbers[count-1] + ":";
}
}
这应该调用返回时,你的要求是什么:
This should return what you requested when calling:
result = Fix.Execute(largeText);
这篇关于发现在一个特定的格式事件再度发生串数发生在给定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!