Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all

Please tell me how to write the below in RegX

String a = "this is a test string"
string b = "Use this as a test string in application"

if ( a || b ) //i.e. a or b . how to write this in expression form
{
.....
.......
.........
}
Thank you
Posted
Comments
Maciej Los 19-Oct-15 2:53am    
Regex? What kind of pattern do you want to use?
Style-7 19-Oct-15 4:56am    
http://stackoverflow.com/questions/4999064/regex-for-string-contains

1 solution

If you want to use regular expressions in your code it is a good idea to learn how it works, because otherwise you will run into trouble if you need change the expression later on.
Here is a web site I usually go to when I need to learn more. Regular Expressions Tutorial[^]

It is also good to have a regex tool where you can test different expressions on a variety of input data. Here is a free tool I usually use: http://sourceforge.net/projects/regextest/[^]

To solve your problem I would create a regular expression that can match both strings
\ba test string\b

The word boundaries \b is used to make sure the words appear in the correct sequence.

The c# code will look something like this:
C#
Regex regex = new Regex(@"\ba test string\b", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
if (regex.IsMatch(a) || regex.IsMatch(b))
{
    // do some work;
}


This example is of course limited by the data you have given in your question.
 
Share this answer
 

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