dongquepao8653
2018-11-08 15:40 阅读 63

编写高级正则表达式

Firstly sorry for my bad English. I'm newbie with regex. I'm trying to make auto translation. For example I have a text like:

/begin CHARACTERISTIC

    AccPed_trqEngLow_MAP
    "Kennfeld für AWD"
    MAP
    0x801B425C
    Map_Xs16Ys16Ws16
    1500.000
    Trq
    -500.0000
    1000.000

    FORMAT "%8.3"
    EXTENDED_LIMITS -3276.800 3276.700


    /begin AXIS_DESCR

        STD_AXIS
        Epm_nEng
        EngN
        16
        0.00
        10000.00

        FORMAT "%8.2"
        EXTENDED_LIMITS -16384.00 16383.50
        DEPOSIT ABSOLUTE

    /end AXIS_DESCR

    /begin AXIS_DESCR

        STD_AXIS
        AccPed_rAPP
        Prc
        8
        0.00
        100.0000

        FORMAT "%8.4"
        EXTENDED_LIMITS -400.0000 399.9878
        DEPOSIT ABSOLUTE

    /end AXIS_DESCR

/end CHARACTERISTIC

I need to select all strings in "" like "Kennfeld für AWD", but do not select where starts with % ("%8.3") Is it possible at least? Thanks in advance.. P.S string may contain quotation marks inside, like "Hello "world" !" It is very difficult for me.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpict99695329 dpict99695329 2018-11-08 16:15

    /\"([^%].*)\"/g matches all strings in quotes that don't start with a % symbol.

    In javascript (es6) it would be:

    const example = `
    /begin CHARACTERISTIC
    
        AccPed_trqEngLow_MAP
        "Kennfeld für AWD"
        MAP
        0x801B425C
        Map_Xs16Ys16Ws16
        1500.000
        Trq
        -500.0000
        1000.000
    
        FORMAT "%8.3"
        EXTENDED_LIMITS -3276.800 3276.700
    
    /end CHARACTERISTIC
    `
    
    let re =  /\"([^%].*)\"/g;
    let results = example.match(re);
    
    console.log(results)
    
    点赞 评论 复制链接分享
  • doupian9490 doupian9490 2018-11-08 16:02

    How about?

    <?php
    $string = '
    WHATVER "Kennfeld für AWD"
    FORMAT "%8.3"
    "Hello "World" !"
    FOO
    BAR
    FORMAT "%8.2"
    FORMAT "%8.4"
    /end CHARACTERISTIC';
    
    preg_match_all('/"((?!\%).+)"/', $string, $matches, PREG_PATTERN_ORDER);
    
    echo "<pre>";
    print_r($matches);
    echo "</pre>";
    
    点赞 评论 复制链接分享

相关推荐