tags:

views:

58

answers:

3

For example, we just want to get the data inside double quotes from a list of strings:

String result = string.Empty;

List<string> strings = new List<string>();
strings.add("string=\"string\"");
strings.add("stringwithspace=\"string with space\"");
strings.add("untrimmedstring=\" untrimmed string\"");
strings.add("anotheruntrimmedstring=\"here it is            \"");
strings.add("number=\"13\"");
strings.add("blank=\"\"");
strings.add("whitespace=\"   \"");

StringBuilder z = new StringBuilder();
foreach(string x in strings){
    if(x has a string inside the doublequote){ //do the regex here
        z.append(x + "\n");
    }
}

and the result would be:

string="string"
stringwithspace="string with space"
untrimmedstring="untrimmed string"
anotheruntrimmedstring="here it is"
number="13"

Any way we could do this?

Or are there some other easier and more optimized ways than using regex?

Thanks a lot.

A: 

This should help, replace [] with "" http://stackoverflow.com/questions/1811183/how-to-extract-the-contents-of-square-brackets-in-a-string-of-text-in-c-using-re/1811202

Paul Creasey
I have tried if (Regex.IsMatch(item, "\".+?\"")){z.Append(item);}... But i cant check and remove white spaces... Thanks for the help, by the way.
Jronny
A: 
var regex = new Regex(@"\""([^\""]*)\""");
foreach(string x in strings)
{
    var matches = regex.Matches(x);
    if (matches.Count > 0)
    {
        var matchedValue = matches[0].Groups[1].Value;
        z.Append(matchedValue);
    }
}
Darin Dimitrov
this does not seem to work... This also returns blank and whitespaces... =)
Jronny
+1  A: 

If the string inside the double quotes are guaranteed not to have a double quotes character, you can use the following regex to capture the trimmed string inside the double quotes.

^(\w+)="\s*([^"]+)\s*"$

$2 or matches[2] will contain the trimmed string - make sure its length is not zero and replace the whole string with $1="$2" and append it (I don't speak c#)

This regex assumes that

  1. The part before = is a single word consisting of only alphanumeric characters and underscores.
  2. The string inside the double quotes are free of double quotes.
Amarghosh
shades of php, is it? =) Thanks for the idea...
Jronny
I do speak some php, but the $2 in the answer is not php specific - $n ($1, $2 etc) is the way of referring to the groups captured using parentheses in regex. For example: matching `Jan-1999` using `(\w{3})-\d{4}` and replacing it with `$1-2000` will give you `Jan-2000` in most of the regex flavors.
Amarghosh