首页 > 解决方案 > Textwrangler 中的正则表达式 - 删除两个字符之间的字符串

问题描述

我有一个文本文件,其中包含多个热门城市的天气统计数据,其中不仅包括当天的高点和低点,还包括昨天的天气,如下所示:

City/Town, State;Yesterday’s High Temp (F);Yesterday’s Low Temp (F);Today’s High Temp (F);Today’s Low Temp (F);Weather Condition;Wind Direction;Wind Speed (MPH);Humidity (%);Chance of Precip. (%);UV Index

Atlanta, GA;43;22;44;22;Partly sunny, chilly;NW;9;38%;7%;4
Atlantic City, NJ;45;24;37;9;A snow squall;WNW;22;36%;58%;3
Baltimore, MD;40;23;34;8;A snow squall, windy;NW;19;37%;57%;1
Bismarck, ND;-10;-29;-8;-15;Frigid;SSE;6;73%;58%;2

我希望能够输入一个正则表达式命令,该命令将删除状态后的前两个数字,删除昨天的高温和低温,让它看起来像这样:

City/Town, State;Yesterday’s High Temp (F);Yesterday’s Low Temp (F);Today’s High Temp (F);Today’s Low Temp (F);Weather Condition;Wind Direction;Wind Speed (MPH);Humidity (%);Chance of Precip. (%);UV Index

Atlanta, GA;44;22;Partly sunny, chilly;NW;9;38%;7%;4
Atlantic City, NJ;37;9;A snow squall;WNW;22;36%;58%;3
Baltimore, MD;34;8;A snow squall, windy;NW;19;37%;57%;1
Bismarck, ND;-8;-15;Frigid;SSE;6;73%;58%;2

是否有捷径可寻?

标签: regexstringtextwrangler

解决方案


比赛部分:

-?\d+;-?\d+;(-?\d+;-?\d+)

替代品:

$1

分解它:

Check for possible hyphen
-?
Check for number
\d+
Check for semicolon
;
Do the above again
-?\d+;
Start of capturing group
(
Do above check 2 times again
-?\d+;-?\d+
End of capturing group
)

$1表示将其替换为第一个捕获组的内容。

如果您不想做任何替换,也可以使用它:

-?\d+;-?\d+;(?=-?\d+;-?\d+)

它利用前瞻来检查前面是否还有两个数字。


推荐阅读