我有一个包含以下标题和(示例)数据的CSV文件:
StopName,RouteName,Travel_Direction,Latitude,Longitude
StreetA @ StreetB,1 NameA,DirectionA,Lat,Long
StreetC @ StreetD,1 NameA,DirectionA,Lat,Long
...
StreetE @ StreetF,1 NameA,DirectionB,Lat,Long
StreetG @ StreetH,1 NameA,DirectionB,Lat,Long
...
StreetI @ StreetJ,2 NameB,DirectionC,Lat,Long
StreetK @ StreetL,2 NameB,DirectionC,Lat,Long
...
StreetM @ StreetN,2 NameB,DirectionD,Lat,Long
StreetO @ StreetP,2 NameB,DirectionD,Lat,Long
.
.
.
我想使用正则表达式(目前在Notepad ++中)获得以下结果:
1 NameA - DirectionA=[[StreetA @ StreetB,[Lat,Long]], [StreetC @ StreetD,[Lat,Long]], ...]
1 NameA - DirectionB=[[StreetD @ StreetE,[Lat,Long]], [StreetF @ StreetG,[Lat,Long]], ...]
2 NameB - DirectionC=[[StreetH @ StreetI,[Lat,Long]], [StreetJ @ StreetK,[Lat,Long]], ...]
2 NameB - DirectionD=[[StreetL @ StreetM,[Lat,Long]], [StreetN @ StreetO,[Lat,Long]], ...]
.
.
.
使用正则表达式和替换,
RgX: ^([^,]*),([^,]*),([^,]*),(.*)
Sub: $2 - $3=[$1,[\4]]
Demo: https://regex101.com/r/gS9hD6/1
我已经走到了这一步:
1 NameA - DirectionA=[StreetA @ StreetB,[Lat,Long]]
1 NameA - DirectionA=[StreetC @ StreetD,[Lat,Long]]
1 NameA - DirectionB=[StreetE @ StreetF,[Lat,Long]]
1 NameA - DirectionB=[StreetG @ StreetH,[Lat,Long]]
2 NameB - DirectionC=[StreetI @ StreetJ,[Lat,Long]]
2 NameB - DirectionC=[StreetK @ StreetL,[Lat,Long]]
2 NameB - DirectionD=[StreetM @ StreetN,[Lat,Long]]
2 NameB - DirectionD=[StreetO @ StreetP,[Lat,Long]]
在一个新的正则表达式中,我尝试将上述结果拆分为" =",但是不知道从那里去哪里。
我认为,获得理想结果的一种方法是在#34; ="之前保留第一个唯一的实例,用"替换新行,"并用[..]括起来使它成为一个数组形式。
修改 大约有10k站(总计),但只有大约100条独特的路线。
编辑2:(也许我现在要求进行太多更改)
对于第一个正则表达式:
在第二次正则表达式更换开始时,
1
NameA - DirectionA=[StreetA @ StreetB, ...]
?1 NameA - DirectionA=[[Lat,Long]]
?答案 0 :(得分:3)
<强> 1 即可。第一次更换:
^([^,]*),([^,]*),([^,]*),(.*)
\2 - \3=[[\1,[\4]]]
<强> 2 即可。第二次更换:
^[\S\s]*?^([^][]*=)\[\[.*\]\]\K\]\R\1\[(.*)\]$
, \2]
第3 即可。重复步骤 2 ,直到不再出现。
ceiling(log2(N))
)。< / LI>
我在步骤1中修改了你的正则表达式,添加了一对括号,用于包围整个集合。
对于步骤 2 ,它会找到同一 Direction 的一对行,将最后一行追加到上一行。
^[\S\s]*?^([^][]*=) #Group 1: captures "1 NameA - DirA="
\[\[.*\]\] #matches the set of Stops - "[[StA @ StB,[Lat,Long]], ..."
\K #keeps the text matched so far out of the match
\]\R #closing "]" and newline
\1 #match next line (if the same route)
\[(.*)\]$ #and capture the Stop (Group 2)
答案 1 :(得分:0)
试试这个 我用移动记事本检查过没有错误。
找什么:
(s.+@\s\w+),(\d{1,} \w+),(\w+),(.+)
替换为:
\2 - \3=[[\1,[\4],...]]