Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:38:21+00:00 2026-05-31T04:38:21+00:00

I’m using this regex to parse lines of a CSV in APEX: Pattern csvPattern

  • 0

I’m using this regex to parse lines of a CSV in APEX:

Pattern csvPattern = Pattern.compile('(?:^|,)(?:\"([^\"]+|\"\")*\"|([^,]+)*)');

It works great, but returns two groups for each match (one for the quoted values, and one for non-quoted values). See below:

Matcher csvMatcher = csvPattern.matcher('"hello",world');
Integer m = 1;
while (csvMatcher.find()) {
    System.debug('Match ' + m);
    for (Integer i = 1; i <= csvMatcher.groupCount(); i++) {
        System.debug('Capture group ' + i + ': ' + csvMatcher.group(i));
    }
    m++;
}

Running this code will return the following:

[5]|DEBUG|Match 1
[7]|DEBUG|Capture group 1: hello
[7]|DEBUG|Capture group 2: null
[5]|DEBUG|Match 2
[7]|DEBUG|Capture group 1: null
[7]|DEBUG|Capture group 2: world

I’d like for each match to only return the non-null capture. Is that possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:38:23+00:00Added an answer on May 31, 2026 at 4:38 am

    This is actually a difficult thing to do.
    It could be done with lookahead/behind assertions.
    Not very intuitive though.

    It looks something like this:
    (?:^|,)(\s*"(?=(?:[^"]+|"")*"\s*(?:,|$)))?((?<=")(?:[^"]+|"")*(?="\s*(?:,|$))|[^,]*)

    How it works is to line up the text body after the first quote " on a valid quoted field. If its not a valid quoted field, it lines up on the quote itself. At that point the text body can be captured as either an un-quoted field, or as a quoted field minus the quotes, in a single capture buffer.

    This is probably a power regex that instruments a precise solution without the need for residual code. I could be missing something, but I see no way to do this without lookaround assertions. So, your engine must support that. If not, you’ll have to pick it out like your solution above.

    Here is a prototype in Perl, with a commented expanded regex below it.
    Good luck!

    $samp = '  "hello " , world",,me,and,th""is, or , "tha""t"  ';
    
    $regex = '
      (?: ^ | , )
      (\s*" (?= (?:[^"]+|"")* " \s*(?:,|$) ) )?
      (
         (?<=") (?:[^"]+|"")* (?="\s*(?:,|$) )
       |
         [^,]*
      )
    ';
    while ($samp =~ /$regex/xg)
    {
       print "'$2'\n";
    }
    

    Output

    'hello '
    ' world"'
    ''
    'me'
    'and'
    'th""is'
    ' or '
    'tha""t'
    

    Commented

    (?: ^ | , )          # Consume comma (or BOL is fine)
    
    (                    # Capture group 1, capture '"' only if a complete quoted field
       \s*                  # Optional many spaces
       "
       (?=                  # Lookahead, check for a valid quoted field, determines if a '"' will be consumed
          (?:[^"]+|"")*
          "
          \s*
          (?:,|$)
       )
    )?                   # End capt grp 1. 0 or 1 quote
    
    (                    # Capture group 2, the body of text
       (?<=")                 # If there is a '"' behind us, we have consumed a '"' in capture grp 1, so this is valid
       (?:[^"]+|"")*
       (?="\s*(?:,|$) )
     |                      # OR,
       [^,]*                  # Just get up to the next ',' This could be incomplete quoted fields
    )                    # End capt grp 2
    

    Extension

    If in fact you might use this, it can be sped up to consume a backreferenced quoted field
    instead of matching a quoted field twice. Backreferences usually resolve to a single string
    comparison api such as strncmp() in C language, making it much faster.
    As a side note, whitespace before/after the field body of non-quoted fields, can be trimmed
    within the regex with a little extra notation.
    Good luck!

    Compressed

    (?:^|,)(?:\s*"(?=((?:[^"]+|"")*)"\s*(?:,|$)))?((?<=")\1|[^,]*)

    Expanded

    (?: ^|, )
    (?: \s* " (?=  ( (?:[^"]+|"")* )  " \s*  (?: ,|$ )  ))?
    ( (?<=") \1 | [^,]* )
    

    Expanded with comments

    (?: ^ | , )          # Consume comma (or BOL is fine)
    
    (?:                  # Start grouping
       \s*                  # Spaces, then double quote '"' (consumed if valid quoted field)
       "                    #
       (?=                  # Lookahead, nothing consumed (check for valid quoted field)
          (                     # Capture grp 1
             (?:[^"]+|"")*          # Body of quoted field  (stored for later consumption)
          )                     # End capt grp 1
          "                     # Double quote '"'
          \s*                   # Optional spaces
          (?: , | $ )           # Comma or EOL
       )                    # End lookahead
    )?                   # End grouping, optionaly matches and consumes '\s*"'
    
    (                    # Capture group 2, consume FIELD BODY
       (?<=")                 # Lookbehind, if there is a '"' behind us the field is quoted
       \1                     # Consume capt grp 1
     |                      # OR,
       [^,]*                  # Invalid-quoted or Non-quoted field, get up to the next ','
    )                    # End capt grp 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.