Reputation: 35
I have an ASP.net string, and I am trying to extract ID from it. Here is the code:
public static string getName(string line)
{
string ret = "";
if (!line.Contains("ID="))
return ret;
var regex = new Regex("/.*ID=\".*?\".*/g");
if (regex.IsMatch(line))
ret = regex.Match(line).Groups[1].Value;
return ret;
}
And regex.IsMatch(line) always returns false.
Upvotes: 1
Views: 130
Reputation: 35
Solved it. Workiing code is :
public static string getName(string line)
{
string ret = "";
if (!line.Contains("ID="))
return ret;
var regex = ".*ID=\"(.*?)\".*";
if (Regex.IsMatch(line, regex) )
ret = Regex.Match(line, regex).Groups[1].Value;
return ret;
}
Upvotes: 0
Reputation: 39443
You didn't do the grouping at your regex. Here it is
var regex = new Regex("/.*ID=\"(.*?)\".*/g");
^ ^
Update: The way you are matching the regex is not correct. Here is how it works.
var regex = "ID=\"(.*?)\"";
if ( Regex.IsMatch(line, regex) ){
ret = Regex.Match(line, regex).Groups[1].Value;
}
Upvotes: 7