Parsing CSV input with a RegEx in java

后端 未结 4 2164
无人及你
无人及你 2020-11-27 16:30

I know, now I have two problems. But I\'m having fun!

I started with this advice not to try and split, but instead to match on what is an acceptable field, and expa

4条回答
  •  鱼传尺愫
    2020-11-27 16:50

    (?:^|,)\s*(?:(?:(?=")"([^"].*?)")|(?:(?!")(.*?)))(?=,|$)
    

    This should do what you want.

    Explanation:

    (?:^|,)\s*
    

    The pattern should start with a , or beginning of string. Also, ignore all whitespace at the beginning.

    Lookahead and see if the rest starts with a quote

    (?:(?=")"([^"].*?)")
    

    If it does, then match non-greedily till next quote.

    (?:(?!")(.*?))
    

    If it does not begin with a quote, then match non-greedily till next comma or end of string.

    (?=,|$)
    

    The pattern should end with a comma or end of string.

提交回复
热议问题