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

What i want to achieve is I have a string

string text = "where name='nilesh' or lastname='nilesh' or distance='nilesh' or gender='nilesh'";

from this I want to check if distance is available in the string and if its available I actually want to check if his value is numeric or not , so I want to check distance value is numeric or not and based on that I want to remove distance='nilesh' if not numeric and if yes I want to keep as it is

please suggest
Posted
Updated 30-Nov-15 20:14pm
v2
Comments
BillWoodruff 30-Nov-15 23:50pm    
How can 'distance (a numeric unit) ever have the value "nilesh" ?
Torakami 30-Nov-15 23:54pm    
actually , progamatically I am creating this string which will then get fire into sql , once this string gets created I want to check the value of distance
BillWoodruff 1-Dec-15 0:13am    
You need to make your question more specific.

1 solution

You can try to use a regular expression, and if it is a match you replace the match with an empty string.

Like this:
[UPDATE] Added 'or\s' to the expression, to get rid of double or's.
[UPDATE2] Updated the regex to match the actual requirement.
C#
private static Regex expression = new Regex(@"or\s*distance=\s*'[a-z]+'", RegexOptions.IgnoreCase);

The expression basically means that you find the word distance followed by = and then 'any letter' within the quotes.
The match will occur in any place within the string.
\s* means zero or more white spaces
C#
string s = @"where name='nilesh' or lastname='nilesh' or distance='nilesh' or gender='nilesh' where name = 'nilesh' or lastname = 'nilesh' or distance = '1' or gender = 'nilesh'";
string t = expression.Replace(s, "");

the result is
where name='nilesh' or lastname='nilesh' or gender='nilesh' where name = 'nilesh' or lastname = 'nilesh' or distance = '1' or gender = 'nilesh'


I don't know if you use quotes around numeric values or not, but it doesn't matter for the functionality.

When working with regular expressions it is good to have tool where you can test the expressions. I use this one, RegExTest[^], but there are other tools out there as well.

To learn about regular expressions you can use this site: http://www.regular-expressions.info/[^]
 
Share this answer
 
v5
Comments
Torakami 1-Dec-15 0:20am    
ok , but before replcing I just want to check distance value wether its numeric or not , if its numeric then I dont want to remove distance = [a-z..] .. whatever ... only if its non numeric I want to replace the complete distance= 'nilesh' to blank in that string

and other thing is , when i place this

private static Regex expression = new Regex(@"distance='[a-z]+'\sor\s", RegexOptions.IgnoreCase);

into my metho it start giving me some bracket related errors not understading why .
} is required its saying
George Jonsson 1-Dec-15 1:10am    
Of course you get an error.
private static Regex expression = new Regex(@"distance='[a-z]+'\sor\s", RegexOptions.IgnoreCase); means it is a class member variable, that is not declared inside the method. That's kind of basic.
(It would be a waste of time to create the regex every time)

The thing with the regex is that it does the check if distance is non numeric, and if so replaces the text distance='nilesh' with an empty string.
If the distance is numeric, nothing is changed.

Didn't you check the output string?

Try the code and see if it behaves the way you want it.
Torakami 1-Dec-15 2:03am    
Hii ,

ok this is working ,, can you please help me a bit ..

my actual string is
SuggestedBy= 'gh' or FromLocation= 'gh' or ToLocation= 'gh' or Distance= 'gh'

can you please show me regex for this string.. I am but confuse with \s\or\s..

in my actual string my distance part i coming at last

Please suggest me regex expression of the same which will replace
" or distance= 'gh' "

thanks brother
George Jonsson 1-Dec-15 2:09am    
Sigh, if you don't show the actual input how am I supposed to be able to help you?
See my updated answer.
Now you are on your own.
Torakami 1-Dec-15 2:26am    
Thanks a lot man .. you are the genius of regex expression.
I really got very confused when there is need to create custom expressions.

Thanks a lot brother . you saved my life and time

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