【问题标题】:Regex in Textwrangler - Remove String Between Two CharactersTextwrangler 中的正则表达式 - 删除两个字符之间的字符串
【发布时间】:2019-01-31 03:01:05
【问题描述】:

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

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

有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: regex string textwrangler


    【解决方案1】:

    比赛部分:

    -?\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+)
    

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

    【讨论】:

    • 我输入了您的匹配部分命令,它删除了昨天和今天的高点和低点,所以很遗憾它对我不起作用。我只想去掉昨天的温度,保留今天的
    • 成功了!太棒了,非常感谢!,我会截屏,这样我可以从你的解释中学习
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2017-06-20
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多