RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Parse Comma Delimited Strings
Expression
^(?<field1>[^,]+),(?<field2>[^,]+),(?<field3>[^,]+)$
Description
Example of a regular expression that can be used to parse a comma delimited string into constituent, named sub-strings. Add or remove regex subfield definitions (?&lt;field1&gt;[^,]+) and corresponding delimiting characters in the regular expression as necessary to accommodate the string and subfields you intend to parse. Optionally, rename the subfield names in the regular expression to more meaningful names if you are using the parsed sub-strings in a program. Replace all occurrences of the comma in the regular expression with any required alternative delimiting character. As long as the delimiting character used does not occur naturally in any of the sub-strings, you should be good-to-go. NOTE: To change the delimiter character you must replace all occurrences of the comma in the current regex (of which there are 5) with the new delimiter character. e.g. (?&lt;field1&gt;[^;]+); changes the delimiter character to a semi-colon for the first field.
Matches
Fred,Barnie,Wilma | Bob,1/1/2003,222-3333 | 100,200,300
Non-Matches
Fred,Barnie,Wilma,Betty | Bob;1/1/2003;222-3333 | Tom Dick Harry
Author Rating: Not yet rated. Jerry Schmersahl
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: what about quote marks
Name: peter
Date: 8/24/2007 10:11:16 AM
Comment:
This pattern completely fails the "what if there is a comma inside a quote". So eventually you're going to run into trouble--probably sooner rather than later.


Title: -1
Name: nicky
Date: 1/19/2004 12:00:00 PM
Comment:
dude, this doesn't work.


Copyright © 2001-2018, RegexAdvice.com | ASP.NET Tutorials