我做的,用正則表達式來發現這句法字的文章微調:文章微調與2層
{word1|word2}
,然後在他們分手「|」,但我需要一種方法來使其支持2級括號,如:
{{word1|word2}|{word3|word4}}
什麼我的代碼做當這樣的線呈現,是採取「{{字詞1 |單詞2}」和「{WORD3 | word4}」,這是不是意。
我想要的是當用這樣一行代碼時,我的代碼將其分解爲「{word1 | word2} | {word3 | word4}」,以便我可以將它與原始函數一起使用並將其分解爲實際的話。
我正在使用c#。
這裏是它如何可能看起來像僞代碼:
Check string for regex match to "{{word1|word2}|{word3|word4}}" pattern
If found, store each one as "{word1|word2}|{word3|word4}" in MatchCollection (mc1)
Split the word at the "|" but not the one inside the brackets, and select a random one (aka, "{word1|word2}" or "{word3|word4}")
Store the new results aka "{word1|word2}" and "{word3|word4}" in a new MatchCollection (mc2)
Now search the string again, this time looking for "{word1|word2}" only and ignore the double "{{" "}}"
Store these in mc2.
I can not split these up normally
這裏是我用來搜索正規表達式「{字詞1 |單詞2}」:
Regex regexObj = new Regex(@"\{.*?\}", RegexOptions.Singleline);
MatchCollection m = regexObj.Matches(originalText); //How I store them
希望有人能幫助,謝謝!
編輯:我解決了這個使用遞歸方法。我正在構建一個文章微調btw。
真是一個令人困惑的問題(?) - 它可以幫助我們找到一個解決方案,如果我們知道你在用'word1' 'word2'數據 - 也許有一個解決方案,不涉及正則表達式(?) – 2012-03-01 15:29:59