【问题标题】:How to Replace with Line Feed in VBScript RegEx如何在 VBScript RegEx 中用换行符替换
【发布时间】:2018-07-23 09:26:05
【问题描述】:

我正在使用 VBScript 并且有一个将 xml 转换为文本文件的脚本。

我正在尝试将字符串 ###EntryEnd###\| 替换为 LF 字符。

我在替换模式中尝试了\n\x0a,但它们不起作用。我发现的唯一解决方法是改用Chr(10)

我一直在寻找此行为的答案,但无法找到它。 \n\x0a 都应该可以工作。有什么建议吗?

代码如下:

' Method to process the file
Private Function PrepFile(ByVal strInp)
    With New RegExp
        .Global = True
        .Pattern = "\|"
        strInp = .Replace(strInp, "")
        .Pattern = "<xmldoc .*?xml:lang=""([^""]+)"">"
        strInp = .Replace(strInp, "English|$1|Part Of Speech|Note|EngDef|Glossary Definition###EntryEnd###|")
        .Pattern = "<remove>.*?</remove>"
        strInp = .Replace(strInp, "")
        .Pattern = "(<tab/>|</para>)"
        strInp = .Replace(strInp, "|")
        .Pattern = "<[^>]*>"
        strInp = .Replace(strInp, "")
        .Pattern = "\n"
        strInp = .Replace(strInp, "")
        .Pattern = "###EntryEnd###\|"
        strInp = .Replace(strInp, chr(10))
    End With
    PrepFile = strInp
End Function

示例文件sn-p:

<?xml version="1.0" encoding="UTF-8"?>
<xmldoc source="" type="TERMS" xml:lang="hu-HU">
<para id="13" name="Entry"><notrans><seg>School Administrator</seg><tab/></notrans><remove>___________</remove><seg>iskolavezető</seg></para>
<para id="14" name="Usage"><notrans><seg> </seg><tab/></notrans><remove>HASZNÁLAT:</remove><seg> </seg></para>
<para id="15" name="EntryText"><notrans><seg> </seg><tab/></notrans><remove>MEGHATÁROZÁS:</remove><seg> </seg></para>
<para id="16" name="Context"><remove>PÉLDA:</remove><remove><seg>Cathy Brown iskolavezető</seg></remove><notrans>###EntryEnd###</notrans></para>
<para id="17" name="Entry"><notrans><seg>School Resource Officer</seg><tab/></notrans><remove>___________</remove><seg>iskolarendőr</seg></para>
<para id="18" name="Usage"><notrans><seg> </seg><tab/></notrans><remove>HASZNÁLAT:</remove><seg> </seg></para>
<para id="19" name="EntryText"><notrans><seg>a law enforcement officer who is responsible for providing security and crime prevention services in schools in parts of the United States and Canada.|</seg><tab/></notrans><remove>MEGHATÁROZÁS:</remove><seg>rendőr, aki azért felelős, hogy az iskolákban biztonsági és bűnmegelőzési feladatokat lásson az Egyesült Államok és Kanada egyes területein.</seg></para>
<para id="20" name="Context"><remove>PÉLDA:</remove><remove><seg>Ocalai iskolarendőrök</seg></remove><notrans>###EntryEnd###</notrans></para>
</xmldoc>

【问题讨论】:

    标签: regex replace vbscript substitution metacharacters


    【解决方案1】:

    在您的问题中,“问题”(只是错误的假设)可以在

    中找到
    • \n\x0a 都应该可以工作

    Replace 方法的documentation 没有声明替换字符串允许使用转义序列,除了$1$2、...对正则表达式模式中捕获组的引用.

    因此,如果 RegExp 对象在替换字符串中没有提供这种行为,并且由于 VBScript 解析器不处理字符串中的任何转义序列,除了转义的双引号,没有任何元素处理 \n 到换行转换。

    您可以使用指示的转义序列来表示搜索模式字符串中的非打印字符,但它们不会被视为替换字符串中的转义序列。

    如果您不喜欢Chr(10) 函数调用,可以使用可用的vbLf 常量来引用换行符

    strInp = .Replace(strInp, vbLf)
    

    【讨论】:

    • 有趣。我找不到任何说明 only 允许子表达式反向引用的文档。当然,如果不处理转义,它们将不起作用,这是合乎逻辑的,但不知道是否只有我跳出这个结论,基本转义会起作用。我猜这是 VB 的缺点,而不是 Regexp 的缺点。
    • @ib11,VBScript 不处理字符串文字中的转义序列,它不是语言的一部分。 RegExp Pattern 属性 can use espace 序列。 Replace 方法使用additionaly allow 子表达式引用的替换字符串。
    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多