Похоже, вы выполняете два поиска и замены:
- Замените каждый номер CC на
*
и последние 4 цифры - Замените любой другой "CC-ish "число на том же элементе с
*
.
Один из подходов состоит в том, чтобы заставить XLinq работать немного сложнее для вас:
// you're not using the elements, ignore them, just get the attributes
foreach (var atr in xelt.Descendants()
.Where(e => e.Attributes()
.Any(a => a.Value.Length >= 13
&& a.Value.Length <= 16))
.SelectMany(e => e.Attributes()))
{
// static basicDigits = new Regex(@"\b\d+\b", RegexOptions.Compiled);
// static ccDigits = new Regex(@"\b\d{13,16}\b", RegexOptions.Compiled);
if (ccDigits.IsMatch(atr.Value))
{
atr.Value = ccDigits.Replace(
atr.Value,
mm => new String('*', mm.Value.Length - 4)
+ mm.Value.Substring(mm.Value.Length - 4));
}
else
{
atr.Value = basicDigits.Replace(atr.Value, "***");
}
}
// using 150k XML (1k nodes/5k attrs, 3 attr/node avg, avg depth 4 nodes)
// with 10% match rate:
// - 25.7 MB/s (average 100 trials)
// - 61 attributes/ms
Пример ввода XML:
<item f1="abc123abc" f2="helloooo 1234567" f3="abc123abc">
<item f1="abc123abc" f2="helloooo 1234567" f3="abc123abc" real1="4444555566667777" />
<item f1="abc123abc" f2="helloooo 1234567" f3="abc123abc" />
ruBTMjSesurMsP6lK2jg
</item>
Вывод:
<item f1="abc123abc" f2="helloooo 1234567" f3="abc123abc">
<item f1="abc123abc" f2="helloooo ***" f3="abc123abc" real1="************7777" />
<item f1="abc123abc" f2="helloooo 1234567" f3="abc123abc" />
ruBTMjSesurMsP6lK2jg
</item>