Click here to Skip to main content
14,773,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
Please help me to find regex:
I have value like: 'Martin Lucas'
and my input string is : 'My name is Martin Lucas, i am working with Mr ABC'.
now i have to match whether the name 'Martin Lucas' is exist in given string.
please suggest the regular expression

previously i was using

@"(^|\s)" + mentioned_profilename + @"(\s|$)"

this in my regex.. it was working fine. but can not match the value ends with ','
eg if following is my string
hello @Nagsdada KumarDada, @Vishal Shimpi and @Hatim Jawad

in this @Nagsdada KumarDada was not getting match
Updated 7-Jan-13 1:33am
Zoltán Zörgő 7-Jan-13 6:09am
Vishal.Shimpi144 7-Jan-13 6:35am
hi i want to find exact match eg Martin Lucas / Martin Lucas, /

1 solution

A regex is probably overkill: the string.Contains method would do it as well.
But the regex is very, very simple:
Martin Lucas

or possibly

But for real accuracy:
Vishal.Shimpi144 7-Jan-13 6:28am
hi, your solutions is not working for following condition
My input string is "hello @Nagsdada KumarDada, @Vishal Shimpi and @Hatim Jawad"
and i have values like '@Nagsdada KumarDada', '@Vishal Shimpi', '@Hatim Jawad' in my DB. i have applaid for loop but with your regex it finds only '@Vishal Shimpi' this value
OriginalGriff 7-Jan-13 6:33am
Show your loop!
Vishal.Shimpi144 7-Jan-13 6:38am
int useridcount = 0;
string mentioned_profilename = "";
string profilename = "";
string tp = "";
useridcount = getmentioneduserid.Count;
List<string> list = new List<string>();

for (int x = 0; x < useridcount; x++)
tp = getmentioneduserid[x]["PhoughtMentionedUserId"].ToString();
Guid metioneduserid = Guid.Empty;
metioneduserid = new Guid(tp);
mentioned_profilename = '@' + PhinkitBLL.User.GetUserName(metioneduserid);
profilename = PhinkitBLL.User.GetUserProfileName(metioneduserid);

bool contains = Regex.IsMatch(phoughttext, @"(^|\s)" + mentioned_profilename + @"(\s|$)");

if (contains == true)
//string txtPhought = Phought;

phoughttext = Regex.Replace(phoughttext, mentioned_profilename, "" + mentioned_profilename + "");

return phoughttext;
OriginalGriff 7-Jan-13 6:46am
Your regex is different from the ones I gave you. You do not match "Nagsdada KumarDada" because it is followed by a comma, which you do not check for. Do you see the end bit of the last one I gave? It checks for punctuation...
Get a copy of Expresso

It's free, and it examines and generates Regular expressions. It also lets you test them very easily...

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