在Sublime Text中正则表达式搜索和替换

时间:2014-07-17 06:50:21

标签: regex replace sublimetext2

我在Sublime Text中有一个数组,其中美国的每个城市都是值,代码包含州的缩写作为键。看起来有点像这样:

$array = array(
  "United States of America" => array(
        "USAK0001" => "Adak",
        "USAK0002" => "Adak Island",
        "USAK0003" => "Akiachak",
        "USAK0004" => "Akiak",
        "USAK0005" => "Akutan",
        "USAK0006" => "Alakanuk",
        "USAK0007" => "Aleknagik",
        "USAK0008" => "Allakaket",
        "USAL0028" => "Arlington",
        "USAL0029" => "Ashford",
        "USAL0030" => "Ashland",
        "USAL0031" => "Ashville",
        "USAL0032" => "Athens",
        "USAL0033" => "Atmore",
        "USAL0034" => "Attalla",
        "USAL0035" => "Auburn",
        "USAL0036" => "Auburn University",
        "USAL0037" => "Autaugaville",
        "USAL0038" => "Axis",
        "USAL0039" => "Baileyton",
       )
);

我需要做的是搜索和替换,根据在数组键代码中找到双字母状态缩写,将状态名称附加到值。

例如,搜索找到" AK"在钥匙中添加"阿拉斯加"价值,所以他们看起来像这样:

    "USAK0001" => "Alaska, Adak",
    "USAK0002" => "Alaska, Adak Island",
    "USAK0003" => "Alaska, Akiachak",
    "USAK0004" => "Alaska, Akiak",
    "USAK0005" => "Alaska, Akutan",
    "USAK0006" => "Alaska, Alakanuk",

每个密钥长度为8个字符,但我甚至不知道如何开始尝试在Sublime Text中执行此操作。

1 个答案:

答案 0 :(得分:0)

感谢nhahtdh,我很容易用PHP脚本来合并它。它最终变得像下面一样简单,不需要正则表达式! (好哇!)

function merge_states_into_cities( $states, $cities ) {

    foreach ( $cities as $key => $array ) {

        foreach ( $array as $code => $city ) {

            foreach ( $states as $abbrv => $state ) {

                if ( strpos( $code, $abbrv, 2 ) ) {
                    echo '"' . $code . '" => "' . $state . ', ' . $city . '",<br>';
                }

            }

        }
    }

}