简体   繁体   English

有没有办法让VB.NET RegEx.Replace在REPLACEMENT参数中使用特殊字符?

[英]Is there a way to get VB.NET RegEx.Replace to use special characters in the REPLACEMENT argument?

I have VB.NET program that applies user supplied PATTERN and REPLACEMENT arguments to a collection of input strings using RegEx.Replace and special characters in the REPLACEMENT argument are not interpreted. 我有一个VB.NET程序,它使用RegEx将用户提供的PATTERN和REPLACEMENT参数应用于输入字符串的集合.Replace和REPLACEMENT参数中的特殊字符不会被解释。

Is there a way to make RegEx.Replace interpret special characters in the REPLACEMENT string like it does in the PATTERN string? 是否有办法使RegEx.Replace像在PATTERN字符串中那样在REPLACEMENT字符串中解释特殊字符? For example, treat "\\t" as a tab and "\\xAE" or "\®" as (R)? 例如,将“ \\ t”视为选项卡,将“ \\ xAE”或“ \\ u00AE”视为(R)?

In Linux, I get the correct output from sed 在Linux中,我从sed获得了正确的输出

echo Test XXX Replacement | sed 's/XXX/\xAE/'

gives "Test ® Replacement" 给出“测试®替换”

But in VB it just gives me the special character pattern as a literal 但是在VB中,它只是为我提供了特殊字符模式作为文字

Regex.Replace("Test XXX Replacement", "XXX", "\t")
Regex.Replace("Test XXX Replacement", "XXX", "\u00AE")

gives "Test \\t Replacement" and "Test \® Replacement" respectively 分别给出“测试\\ t替换”和“测试\\ u00AE替换”

I've found 2 somewhat related but distinctly not applicable posts, my problem differs from Escape Regex.replace() replacement string in VB.net in that I actually want the special characters in my replacement strings. 我发现2个相关但又不适用的帖子,我的问题与VB.net中的Escape Regex.replace()替换字符串不同,因为我实际上希望 替换字符串中包含特殊字符。

It also differs from Regex VB.Net Regex.Replace , that question had control of the replacement string and dodged my issue by using a VB constant instead of a RegEx special character. 它也不同于Regex VB.Net Regex.Replace ,该问题可以控制替换字符串,并通过使用VB常量而不是RegEx特殊字符来规避我的问题。

Are there any settings/options/utilities/methods that can make my (user supplied!) RegEx REPLACEMENT strings correctly handle special characters? 是否有任何设置/选项/实用程序/方法可以使我的(用户提供!)RegEx REPLACEMENT字符串正确处理特殊字符?

Is there a way to make RegEx.Replace interpret special characters in the REPLACEMENT string like it does in the PATTERN string? 是否有办法使RegEx.Replace像在PATTERN字符串中那样在REPLACEMENT字符串中解释特殊字符? For example, treat "\\t" as a tab and "\\xAE" or "\®" as (R)? 例如,将“ \\ t”视为选项卡,将“ \\ xAE”或“ \\ u00AE”视为(R)?

You mean like the Regex.Unescape(String) Method ? 您是说像Regex.Unescape(String)方法一样

If you can accept the limitations declared in the Remarks Section : 如果您可以接受“ 备注”部分中声明的限制:

  • It reverses the transformation performed by the Escape method by removing the escape character ("\\") from each character escaped by the method. 通过从该方法转义的每个字符中删除转义字符(“ \\”),可以逆转由Escape方法执行的转换。 These include the \\, *, +, ?, |, {, [, (,), ^, $, ., #, and white space characters. 这些包括\\,*,+,?,|,{,[,(,),^,$,。,#和空白字符。 In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters. 另外,Unescape方法取消转义右括号(])和右括号(})字符。
  • It replaces the hexadecimal values in verbatim string literals with the actual printable characters. 它用实际的可打印字符替换逐字字符串文字中的十六进制值。 For example, it replaces @"\\x07" with "\\a", or @"\\x0A" with "\\n". 例如,它将@“ \\ x07”替换为“ \\ a”,或将@“ \\ x0A”替换为“ \\ n”。 It converts to supported escape characters such as \\a, \\b, \\e, \\n, \\r, \\f, \\t, \\v, and alphanumeric characters. 它将转换为受支持的转义字符,例如\\ a,\\ b,\\ e,\\ n,\\ r,\\ f,\\ t,\\ v和字母数字字符。

Regex.Unescape("\\xAE\\t\®") yields the string result of "®" & vbTab & "®" Regex.Unescape("\\xAE\\t\®")产生字符串结果“®”&vbTab&“®”

VB.Net doesn't have escape characters. VB.Net没有转义字符。

According to the docs for the Replace method: 根据文档Replace方法:

Substitutions are the only regular expression language elements that are recognized in a replacement pattern. 替换是在替换模式中唯一可识别的正则表达式语言元素。 All other regular expression language elements, including character escapes, are allowed in regular expression patterns only and are not recognized in replacement patterns. 所有其他正则表达式语言元素(包括字符转义符)仅在正则表达式模式中允许使用,而在替换模式中不识别。

The equivalent to your two lines of code would be: 相当于您的两行代码为:

Regex.Replace("Test XXX Replacement", "XXX", vbTab)
Regex.Replace("Test XXX Replacement", "XXX", ChrW(&H00AE))

You could also use string interpolation with the replacement string if you needed to embed a hex string or character in a longer replacement string: 如果需要在更长的替换字符串中嵌入十六进制字符串或字符,也可以在替换字符串中使用字符串插值:

Regex.Replace("Test XXX Replacement", "XXX", $"{vbTab} yyy {ChrW(&H00AE)}")

Be sure to import the Microsoft.VisualBasic namespace, if not already imported. 如果尚未导入,请确保导入Microsoft.VisualBasic命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM