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 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器