28012

C# Split namevaluepair string with delimitter in values using regex

Question:

I have a string

<blockquote>

"FirstName=John, LastName=Smith, Address=1 Wall Street, NY"

</blockquote>

which needs to be split into a dictionary as:

<ul><li>{"FirstName", "John"} </li> <li>{"LastName", "Smith"} </li> <li>{"Address", "1 Wall Street, NY"}</li> </ul>

How can this be achieved using Regex considering Address field value has the delimiter ',' in it?

Also lets say I have the following string (note the colon in Address):

<blockquote>

"FirstName=John, LastName=Smith, Address:1 Wall Street, NY"

</blockquote>

How can the above mentioned result be with either = or : acting as the key value pair separator?

Answer1:

Assuming keys cannot contain any of delimeters (comma should be followed by key to separate pairs)

var data = "FirstName=John, LastName=Smith, Address:1 Wall Street, NY, USA, TestKey=TestValue"; var dic = new Dictionary<string, string>(); var reg = @"([^=:,]*)[=:](.*?)(?:$|,\s*(?=[^=:,]*[=:]))"; foreach (Match m in Regex.Matches(data, reg)) { var key = m.Groups[1].Value; var val = m.Groups[2].Value; dic[key] = val; Console.WriteLine("{0} = {1}", key, val); }

Recommend

  • How do you run iex from Emacs?
  • How to use Vue Router in Vue 2
  • return value from zmq socket
  • Splitting on comma outside quotes when escaped quotes exist
  • How can I test the validity of a ReferenceProperty in Appengine?
  • Assign different values to cell arrays in MATLAB at once
  • Return value syntax in java
  • Plot a CSV file where the delimiter is '; ' (semicolon + space)
  • How to convert row to column in notepad++
  • Deploying pre-encrypted configuration files to a production environment
  • Locating unmatched delimiters in Clojurescript
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • What does a hyphen at end of a term mean
  • How to split row into many rows in postgresql
  • Efficient User-Agent Regex to find Safari in Python
  • Use sed with regex and (
  • For loop with if condition on multiple R functions
  • Fail:(TESTMODE) Transactions of this market type cannot be processed on this system
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • Abort upload large uploads after reading headers
  • Custom Tabgroup Appcelerator
  • iOS: Detect app start via notification press
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Initializer list vs. initialization method
  • Why value captured by reference in lambda is broken? [duplicate]
  • Retrieving value from sql ExecuteScalar()
  • Regex thinks I'm nesting, but I'm not
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Data Validation Drop Down Box Arrow Disappearing
  • KeystoneJS: Relationships in Admin UI not updating
  • Hits per day in Google Big Query
  • Why joiner is not used after Sequence generator or Update statergy
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS