Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regular Expressions, string between single quotes

Tags:

c#

regex

string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";

i want to get the text between ' quotes using Regular Expressions.

Can anyone?

like image 732
Faizal Balsania Avatar asked Apr 14 '11 11:04

Faizal Balsania


2 Answers

Something like this should do it:

string val = "name='40474740-1e40-47ce-aeba-ebd1eb1630c0'";

Match match = Regex.Match(val, @"'([^']*)");
if (match.Success)
{
    string yourValue = match.Groups[1].Value;
    Console.WriteLine(yourValue);
}

Explanation of the expression '([^']*):

 '    -> find a single quotation mark
 (    -> start a matching group
 [^'] -> match any character that is not a single quotation mark
 *    -> ...zero or more times
 )    -> end the matching group
like image 149
Fredrik Mörk Avatar answered Oct 16 '22 03:10

Fredrik Mörk


You are looking to match GUID's in a string using a regular expression.

This is what you want, I suspect!

public static Regex regex = new Regex(
  "(\\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-"+
  "([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\\}{0,1})",RegexOptions.CultureInvariant|RegexOptions.Compiled);

Match m = regex.Match(lineData);
if (m.Succes)
{
...
}
like image 3
Jaapjan Avatar answered Oct 16 '22 01:10

Jaapjan