我正在处理将所有未知(未知=其他可用)字符替换为由我选择的一个字符(例如'?')的C#代码。可用字符可以是单个字符,或两个或多个字符的序列。替换字符中未包含的所有字符或序列列表
例如:
Input string: [email protected]@CZ
Available characters or sequences: A, B, C, @@
Desired output: [email protected]@C?
其他例如:
Input string: [email protected]
Available characters or sequences: A, B, C, @@
Desired output: A?B??C?
我想要实现这个使用正则表达式表达。我到了解决方案最接近的是这样的正则表达式:
([email protected]@|[ABC]).
但在这样将导致错误的结果输入字符串的例子:
Input string: [email protected]@CZ
Result from above regex: [email protected]?C?
Instead of wanted by me: [email protected]@C?
如何,我可以实现我的目标?
匹配它们并加入匹配值。 –
你能为我的例子发布一个正则表达式吗? – user1558211
问题是,你只消耗一个字符与你的否定前瞻断言。第一个@匹配,但第二个将被删除。你可以用这个正则表达式来实现:['(?<!@)@(?!@)| [^ ABC @]'](http://regexstorm.net/tester?p=(%3f%3c!% 40)%40(%3f!%40)%7c%5b%5eABC%40%5d&i = AXBY%40%40CZ&r =%3f)但使用WiktorStribiżew的提示要简单得多。 –