Splitting by a semicolon not surrounded by quote signs

后端 未结 3 2036
感情败类
感情败类 2021-01-23 11:07

Well, hello community. I\'m workin\' on a CSV decoder in PHP (yeah, I know there\'s already one, but as a challenge for me, since I\'m learning it in my free time). Now the prob

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-23 11:49

    Split is not a good choice for csv type lines.
    You could use the old tried and true \G anchor with a find globally type func.

    Practical

    Regex: '~\G(?:(?:^|;)\s*)(?|"([^"]*)"|([^;]*?))(?:\s*(?:(?=;)|$))~'

    Info:

     \G                            # G anchor, start where last match left off
     (?:                           # leading BOL or ;
          (?: ^ | ; )
          \s*                           # optional whitespaces
     )
     (?|                           # branch reset
          " 
          ( [^"]* )                     # (1), double quoted string data
          "
       |                              # or
          ( [^;]*? )                    # (1), non-quoted field
     )
     (?:                           # trailing optional whitespaces
          \s* 
          (?:
               (?= ; )                       # lookahead for ;
            |  $                             # or EOL
          )
     )
    

提交回复
热议问题