douao1854 2018-04-08 12:33
浏览 20
已采纳

如何从文本文件中提取这些字符?

I have huge amount of code in a text file, for example:

"4EF\"]
,\"blue-apple\^&**%
"4EF\"]
,\"orange\/^4^&**%

How can I extract the following data:

blue-apple
orange

The data is between 4EF\"] ,\" and \ as you can see.

  • 写回答

1条回答 默认 最新

  • dou91808 2018-04-08 12:47
    关注

    You could use preg_match_all() to get the part of the string you want:

    $str = '"4EF\"]
    ,\"blue-apple\^&**%
    "4EF\"]
    ,\"orange\/^4^&**%';
    
    $str = preg_match_all('~^"4EF\\\\"[^"]+"([^\\\\]+)~m', $str, $matches);
    print_r($matches[1]);
    

    The regular expression will skip "4EF\" + all after the next ", then, use a capture group to keep all until the next backslash.

    Or:

    $str = '"4EF\"]
    ,\"blue-apple\^&**%
    "4EF\"]
    ,\"orange\/^4^&**%';
    $str = preg_match_all('~^"4EF\\\\"\]\\\
    ,\\\\"([^\\\\]+)~m', $str, $matches);
    print_r($matches[1]);
    

    Outputs:

    Array
    (
        [0] => blue-apple
        [1] => orange
    )
    

    The regular expression:

    ~          # delimiter
    ^          # indicate that the line begins by the following
    "4EF       # sequence ("4EF)
    \\\\       # a backslash
    "          # a double quote
    \]         # ']' need to be escaped 
    \\\\       # a backslash
    n,         # sequence (n,)
    \\\\       # backslash
    "          # double quote
    (          # start capture group
      [^\\\\]+ # all characters until a backslash
    )          # end capture group
    ~          # end delimiter
    m          # multi line (to use ^) 
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部