Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

i want to satisfy the condition if rawValue contains text or Empty.but its give me error
Error 15 'System.Text.RegularExpressions.Regex' is a 'type' but is used like a 'variable'

C#
public static string ExtractValue(string rawValue)
        {
            if (rawValue == string.Empty || rawValue == Regex)
            {
                return null;
            }
            else
            {
                return new Regex(@"[a-zA-z\s]*").Replace(rawValue, string.Empty);
            }
        }

Please let me know why is this happen .
Thanks in Advance
Harshal
Posted

It is because of this line rawValue == Regex
If you want to match your rawValue matches Regex expression you can do it like this
C#
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
  MatchCollection matches = rgx.Matches(input);
  if (matches.Count > 0)
  {
     //code
  }

Check MSDN article about Regex Class
 
Share this answer
 
v2
Comments
R Harshal 18-Aug-14 6:12am    
HI Hard-Rockz
If i found any character then it should return null.this what i want i am trying to do .
Thanks in advance
Dilan Shaminda 18-Aug-14 6:17am    
As OriginalGriff suggested in below answer comment if the string is empty it doesn't contain any character. So no need to use Regex for that.
Um.
Regex is a type, so you can't compare a string with it:
C#
if (rawValue == string.Empty || rawValue == Regex)
{
    return null;
}
Won't work.
instead, try starting with:
C#
if (string.IsNullOrWhiteSpace(rawValue))
{
    return null;
}
 
Share this answer
 
Comments
R Harshal 18-Aug-14 5:22am    
I want to check for text.Do you have any idea for that.
R Harshal 18-Aug-14 5:26am    
I have try this .
public static string ExtractValue(string rawValue)
{
if (rawValue == string.Empty || Regex(@"[a-zA-z\s]*").Replace(rawValue, null))
{
return null;
}
else
{
return new Regex(@"[a-zA-z\s]*").Replace(rawValue, string.Empty);
}
}

but still i am getting the same error.
OriginalGriff 18-Aug-14 5:47am    
What exactly are you trying to do?
Because at the moment it looks like you are throwing lumps of code around without trying to think about what they do...much less how you should use them.
R Harshal 18-Aug-14 5:56am    
If i found any character then it should return null.this what i want i am trying to do .
Thanks in advance
OriginalGriff 18-Aug-14 6:12am    
If you want "any character" why are you trying to use a regex?
If it isn't empty then it contains a character.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900