Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code NEW20VISION at checkout. Limited time offer.

Quote:
I am getting this type of string from db.I have to find out if there is any alphanumeric word in the string.If it contains any alphanumeric word.I have underline it.Like in the above case it is:NEW20VISION
Posted
Updated 28-Aug-18 1:14am
Comments
Sandeep Mewara 14-May-12 8:04am    
Ok. And what have you tried so far?
Tirthankar Dutta 14-May-12 8:06am    
I have tried the regex like @"^[a-zA-Z0-9]\s*$";
VJ Reddy 23-May-12 6:08am    
Thank you for accepting the solution :)

Try this

C#
string s = TextBox1.Text;
string str = "";
string[] words = s.Split(' ');
foreach (string word in words)
{
    Regex r = new Regex("[a-zA-Z]");

    if (r.IsMatch(word))
    {
        Regex r1 = new Regex("[0-9]");
        if (r1.IsMatch(word))
        {
          str=str +  " " + "<U>" +  word + "</U>";
        }
        else
            str=str +  " " + "" +  word + "";
    }
    else
            str=str +  " " + "" +  word + "";
}

Label1.Text=str;


Hope this helps if yes then accept and vote the answer otherwise revert back with your queries.
--RDBurmon Sr.Software Engineer
 
Share this answer
 
Comments
Rahul Rajat Singh 14-May-12 8:08am    
I am no expert on regex but it seems like the first if condition will fail for the required words we want and all the logic to find them is inside that condition only. I might be wrong, please correct me if I am.
Rahul Rajat Singh 14-May-12 8:15am    
I was wrong. the solution is perfect. I tried this one and it worked fine. +5 from me.


static void Main(string[] args)
{
string s = @"Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code NEW20VISION at checkout. Limited time offer. ";
string str = "";
string[] words = s.Split(' ');
ArrayList foundWords = new ArrayList();

foreach (string word in words)
{
Regex r = new Regex("[a-zA-Z]");

if (r.IsMatch(word))
{
Regex r1 = new Regex("[0-9]");
if (r1.IsMatch(word))
{
foundWords.Add(word);
}
}
}

foreach (string strr in foundWords)
{
Console.WriteLine(strr);
}
}
RDBurmon 14-May-12 8:17am    
Thanks
Tirthankar-Dutta : Try above code and let us know .
Tirthankar Dutta 14-May-12 8:18am    
Thanks all.It worked
VJ Reddy 14-May-12 11:24am    
Good answer. 5!
The Solution 1 given by RDBurmon is good.

As an alternative the following regular expression can be used to search for the alpha numeric words. It first searches for a word with 2 or more alpha numeric characters, then it checks whether at least one digit and at least one character is present in the searched word and only if it is so, the word will be captured as a group.

Further the captured group can be used for replacement. For eg. each of the captured word can be enclosed within <u> and </u> as shown below:

The Regular Expression can be tested here http://regexhero.net/tester/[^]
C#
string inputText = @"Get 20% Off Your Entire Order & FREE Shipping " +
       @"(first time customers only)! Enter coupon code NEW20VISION at checkout." +
       @"Limited time offer. NEW20 20VISION  New20New20New20Ne20  N2";

MatchCollection alphaNumMatches = Regex.Matches(inputText,
       @"(?=\w{2,})(?=\w*\d+)(?=\w*\p{L}+)(\w{2,})",
       RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

foreach(Match match in alphaNumMatches){
    if (match.Groups.Count > 0)
        Console.WriteLine (match.Groups[1].Value);
}

string replacedText = Regex.Replace(inputText,
       @"(?=\w{2,})(?=\w*\d+)(?=\w*\p{L}+)(\w{2,})",
       @"<u>$1</u>");

Console.WriteLine (replacedText);

//Output

//NEW20VISION
//NEW20
//20VISION
//New20New20New20Ne20
//N2
//Get 20% Off Your Entire Order & FREE Shipping (first time customers only)! Enter coupon code <u>NEW20VISION</u> at checkout.Limited time offer. <u>NEW20</u> <u>20VISION</u>  <u>New20New20New20Ne20</u>  <u>N2</u>
 
Share this answer
 
v3
Comments
Maciej Los 14-May-12 15:33pm    
To find all words containing numbers, correct pattern is: \w*([0-9]+)\w*
VJ Reddy 14-May-12 16:31pm    
It's true. But it checks to see that at least one digit is there in the searched word but does not ensure that at least one alphabet is there in the search. For example this pattern matches 20 in the above text, whereas the OP wants that the word should contain at least one digit and at least one alphabet as I understand from the word AlphaNumeric. There comes the problem.
It is interesting. The pattern is short. If it can be modified to give at least one alphabet and one digit in the match, then I think it will be good.
Thank you.

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