2017-07-25 82 views
0

之间更换EOL在多线串是这样的:正则表达式:发现和报价

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." 

我试图在计算器上发现的各种正则表达式相关的解决方案,但我不能给他们适应我想要的东西。

我将在AHK函数中使用这个表达式表达:

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

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

任何帮助表示赞赏。

+0

不,它不会修复它。挑战是只在报价之间进行更换。 – JnLlnd

+0

我会让我的问题更清楚。 – JnLlnd

+0

我的问题不够清楚。我只在标题中提到了“之间的引号”要求,并没有再提出问题本身。对于那个很抱歉。请参阅编辑的问题。 – JnLlnd

回答

0

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

str = 
(
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." 
) 
outStr := "" 
Loop, Parse, str, `" 
{ 
    field := A_LoopField 
    StringReplace, field, field, `r,, All 
    StringReplace, field, field, `n, ¶, All 
    outStr .= field 
} 
MsgBox % outStr 
ExitApp 
+0

我不是在寻找一个“简单替代”。挑战在于只在引号之间进行替换,而不是简单的解决方案所做的全部替换。 – JnLlnd

+0

我会让我的问题更清楚。 – JnLlnd

+0

你必须解析字符串。我修改了我的答案。 – fischgeek

0

因为它似乎只存在没有使用正则表达式的解决方案,我张贴在这里通过maestrith写(AHK上论坛)的解决方案。它确实取代了引号内的EOL,保留了引用封装器。它使用StrSplit来读取和处理整个内容以隔离引用段,并使用RegExReplace和StringReplace的组合来处理它们。我仍然需要在一个非常大的文件上测试它,看看它是如何执行的,与我编写的另一个脚本相比,它一次只处理一个字符。

#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 
0

即使没有回答我的原始问题,我也会将其添加为答案。这不使用正则表达式,但最终比早期答案中的试验性更快(在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解决方案,我将离开此线程而不接受任何答案。永远不知道...

谢谢大家的意见。