我需要替換文本文件中的數字字符串列表。然而,如果字符串是另一個數字字符串的一部分,它不應該被更換:需要更換 字符串:111111 替換字符串:蒙面Regex.Replace - 跳過屬於較長字符串的字符串
Text file:
111111
111111.text text
text text111111 text text
a111111
2111111
111111a
1111112
a111111a
Expected result:
MASKED
MASKED.text text
text textMASKED text text
aMASKED
2111111 -> Character 2 prevents masking
MASKEDa
1111112 -> Character 2 prevents masking
aMASKEDa
這是我的代碼:
inputText = Regex.Replace(inputText, "(?<![0-9])" + stringToMask + "(?<![0-9])", "####MASKED####");
此代碼只是跳過一切,並沒有執行任何掩蔽。
應該不是第二個是 - 定向排除(<[0-9]?!)([0-9]?!)? – sh54 2011-04-12 10:10:51
什麼sh54說。他應該得分:) – sehe 2011-04-12 10:16:45
@亨克,不,你不能。 '\ D'會是'[^ 0-9]'。 '!'屬於斷言'(?<!...)'的後面。 – stema 2011-04-12 10:24:15