正则表达式:发现和报价
问题描述:
之间更换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???, "¶")
任何帮助表示赞赏。
答
你可以解析字符串并以这种方式进行操作吗?
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
答
因为它似乎只存在没有使用正则表达式的解决方案,我张贴在这里通过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
答
即使没有回答我的原始问题,我也会将其添加为答案。这不使用正则表达式,但最终比早期答案中的试验性更快(在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解决方案,我将离开此线程而不接受任何答案。永远不知道...
谢谢大家的意见。
不,它不会修复它。挑战是只在报价之间进行更换。 – JnLlnd
我会让我的问题更清楚。 – JnLlnd
我的问题不够清楚。我只在标题中提到了“之间的引号”要求,并没有再提出问题本身。对于那个很抱歉。请参阅编辑的问题。 – JnLlnd