RegEx:在引号之间查找并替换EOL

时间:2017-07-25 13:38:21

标签: regex autohotkey

在这样的多行字符串中:

She Loves You [Mono],"Past Masters, Vol. 1",4,"She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah, yeah"
Eight Days A Week,Beatles For Sale,8,"Eight days a week
I love you.
Eight days a week
Is not enough to show I care."

我想用引号之间的EOL(\ r \ n)替换为“¶”(ASCII代码182)之类的替换字符,以使该字符串成为单行。

结果将是:

She Loves You [Mono],"Past Masters, Vol. 1",4,"She loves you, yeah, yeah, yeah¶She loves you, yeah, yeah, yeah¶She loves you, yeah, yeah, yeah, yeah"
Eight Days A Week,Beatles For Sale,8,"Eight days a week¶I love you.¶Eight days a week¶Is not enough to show I care."

我在StackOverflow上尝试了各种与RegEx相关的解决方案,但我无法根据自己的需要调整它们。

我将在AHK函数中使用此RegEx表达式:

RegExReplace(Haystack, NeedleRegEx [, Replacement = "", OutputVarCount = "", Limit = -1, StartingPosition = 1])

RegExReplace(MyText, NeedleRegEx???, "¶")

任何帮助表示感谢。

3 个答案:

答案 0 :(得分:0)

你可以解析字符串并以这种方式操作吗?

Neighborhood

答案 1 :(得分:0)

由于似乎没有使用RegEx的解决方案,我在这里发布了由maestrith(在AHK论坛上)编写的解决方案。它确实取代了引号内的EOL,保留了引号封装器。它使用StrSplit读取和处理整个内容,以隔离引用的部分,使用RegExReplace和StringReplace的组合处理它们。我仍然需要在一个非常大的文件上测试它以查看它与我编写的另一个脚本相比如何执行,该脚本一次处理一个char的内容。

#SingleInstance,Force
info=
(
She Loves You [Mono],"Past Masters, Vol. 1",4,"She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah, yeah"
Eight Days A Week,Beatles For Sale,8,"Eight days a week
I love you.
Eight days a week
Is not enough to show I care."
)
for a,b in StrSplit(info,Chr(34)){
    if(!Mod(A_Index,2)){
        replace:=RegExReplace(b,"\R",chr(182))
        StringReplace,info,info,%b%,%Replace%
    }
}
Gui,Font,s10
Gui,Add,Edit,w1000 h200 -Wrap,%Info%
Gui,Show

答案 2 :(得分:0)

即使它没有回答我原来的问题,我也会将其添加为答案。这不使用RegEx,但最后,比早期答案中的暂定速度快(在3兆csv文件上大约快3倍到5倍)。

#SingleInstance,Force
info=
(
She Loves You [Mono],"Past Masters, Vol. 1",4,"She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah
She loves you, yeah, yeah, yeah, yeah"
Eight Days A Week,Beatles For Sale,8,"Eight days a week
I love you.
Eight days a week
Is not enough to show I care."
)
blnInsideEncapsulators := false
Loop, Parse, info
    ; parsing on a temporary copy of info -  so we can update the original info inside the loop
{
    if (A_Index = 1)
        info := ""
    if (blnInsideEncapsulators AND A_Loopfield = "`n")
        info := info . Chr(182)
    else
        info := info . A_Loopfield
    if (A_Loopfield = """")
        blnInsideEncapsulators := !blnInsideEncapsulators ; beginning or end of encapsulated text
}
Gui,Font,s10
Gui,Add,Edit,w1000 h200 -Wrap,%Info%
Gui,Show

如果有人提供完整的RegEx解决方案,我将在没有接受答案的情况下离开此主题。永远都不知道......

感谢大家的投入。