Visual Studio 查找问题并用 RegEx 替换?
Issue with Visual Studio find and replace with RegEx?
我可能在这里失去了理智,但我使用的正则表达式适用于 JavaScript 和其他语言,但不适用于 VS2013。
问题
我想查找所有 phone 号码并将其替换为新的 phone 号码,并在出现的相同位置保留空格。
例子
当前 phone 号码 08453333666 正在被 03035555111 取代。该号码可能表示为 0845 333 3666、08453 3336 66 或任何其他变体。在给出的示例中,我想用相同索引处的空格替换它们,因此 0845 333 3666 变为 0303 555 5111 和 08453 3336 66 变为 03035 5551 11.
正则表达式
我正在使用
0( *)8( *)4( *)5( *)3( *)3( *)3( *)3( *)6( *)6( *)6
对于查找和
01
用于替换。
这似乎适用于 JavaScript 和其他语言,但不适用于 VS2013 在文件中查找和替换。
有什么想法吗?
您需要在替换中使用 ${<BACK-REFERENCE_ID>}
来指示正确的反向引用:
03035555111
见demo
关键是正则表达式引擎看到 </code> 并开始寻找第 13 个捕获的组,由于它丢失了,所以这个 <code>
被视为文字。等等。
见Substituting a Numbered Group:
All digits that follow $
are interpreted as belonging to the number group. If this is not your intent, you can substitute a named group instead. For example, you can use the replacement string 1
instead of </code> to define the replacement string as the value of the first captured group along with the number <code>"1"
.
...
If number does not specify a valid capturing group defined in the regular expression pattern, $number
is interpreted as a literal character sequence that is used to replace each match.
我可能在这里失去了理智,但我使用的正则表达式适用于 JavaScript 和其他语言,但不适用于 VS2013。
问题
我想查找所有 phone 号码并将其替换为新的 phone 号码,并在出现的相同位置保留空格。
例子
当前 phone 号码 08453333666 正在被 03035555111 取代。该号码可能表示为 0845 333 3666、08453 3336 66 或任何其他变体。在给出的示例中,我想用相同索引处的空格替换它们,因此 0845 333 3666 变为 0303 555 5111 和 08453 3336 66 变为 03035 5551 11.
正则表达式
我正在使用
0( *)8( *)4( *)5( *)3( *)3( *)3( *)3( *)6( *)6( *)6
对于查找和
01
用于替换。
这似乎适用于 JavaScript 和其他语言,但不适用于 VS2013 在文件中查找和替换。
有什么想法吗?
您需要在替换中使用 ${<BACK-REFERENCE_ID>}
来指示正确的反向引用:
03035555111
见demo
关键是正则表达式引擎看到 </code> 并开始寻找第 13 个捕获的组,由于它丢失了,所以这个 <code>
被视为文字。等等。
见Substituting a Numbered Group:
All digits that follow
$
are interpreted as belonging to the number group. If this is not your intent, you can substitute a named group instead. For example, you can use the replacement string1
instead of</code> to define the replacement string as the value of the first captured group along with the number <code>"1"
....
If number does not specify a valid capturing group defined in the regular expression pattern,
$number
is interpreted as a literal character sequence that is used to replace each match.