473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Anyone have a Regex pattern for finding name/value pairs in a string?

I have strings returned from a config file that contain zero or more
name/value pairs using a pattern like "name = value;"

I want to extract the value of a given name (passwords, etc.). I want this
to ignore whitespace in the pattern match and not be case sensitive.

Anyone have a pattern that will do this?
Nov 16 '05 #1
1 3037
After some experimentation, I finally wound up with this solution. Although
it works, it does not strike me as elegant and am open to suggestions for
improvement:

public ParseFTPConnectionString(string conn)
{
Regex r = new Regex(@"\s*(\w*)\s*=\s*((\w|\\|\/|\.)*);",
RegexOptions.Compiled);
MatchCollection mc = r.Matches(conn);

foreach (Match m in mc)
{
switch (m.Groups[1].Value.ToLower())
{
case "server":
_Server = m.Groups[2].Value;
break;
case "userid":
_UserID = m.Groups[2].Value;
break;
case "pwd":
_Pwd = m.Groups[2].Value;
break;
case "remotesendpath":
_RemoteSendPath = m.Groups[2].Value;
break;
case "remotercvpath":
_RemoteRcvPath = m.Groups[2].Value;
break;
case "uploadfileprefix":
_UploadFilePrefix = m.Groups[2].Value;
break;
}
}
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
5
by: Tap | last post by:
I would like to parse the following string with as little code as possible. stringValue = "Email Message ID:TAPASVI to...
4
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
4
by: Jéjé | last post by:
Hi, I have a file which contain 1 pair of values by line like: Name1=Value1 = I nned to store these pair of values in a sortedlist. So the result expected for the 2 samples lines is: Key ...
0
by: hardieca | last post by:
Hello, I'm creating a regex pattern that will pull out the attribute and value pairs from an HTML tag. What I have so far is: @"\s*(?<attribute>*)\s*=\s*?(?<value>*)" This is all well and...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
3
by: jwgoerlich | last post by:
Hello group, I am working on a query string class. The purpose is to parse name-value pairs from incoming text. Currently, I am using the Regex code below. I have two questions. First, the...
10
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.