douao1854 2018-04-08 20: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 20: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 ^) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置