ARR01
ARR01

Reputation: 35

Regular expression to match every word starting with another word (including special characters)

I'm using a simple regular expression to match strings which containing words starting with:

string regExp = @"\b" + searchFor; // searchFor is input value to look for
matchName = Regex.IsMatch(recipient.User.FullName, regExp, RegexOptions.IgnoreCase);

It works great for words (of course), but if FullName contain something like:

This is Ex$ample

And user try to looks for Ex$a, then it never matches.

Or if user searchFor is:

$ 

It returns true all the time for the records.

I tried to look in other posts but I can't find something similar.

Thanks

**UPDATED**

Let me try to explain. The idea is to look for names that begin with certain words:

string searchFor = "Gha";

and recipient.User.FullName list contain:

Jordan Ghassari
James Cunningham
Ghabriel Bercholee 

The matches must be:

Jordan Ghassari 
Ghabriel Bercholee 

It is also necessary to consider that fullname list can contain special characters. It needed to include special characters in the search

Search for o^bri and obtain:

O^Brian

Search for #34 and obtain:

Depto #345

Upvotes: 0

Views: 182

Answers (2)

OneHoopyFrood
OneHoopyFrood

Reputation: 3959

I'm not certain of your full question, but you'll need to use a Regex escaper for conditions like this one. The $ is a special character. It would be \$ to actually search for a dollar sign. You should look up other special characters and be aware of them too, though C# regex escaper will do the tick.

EDIT Like this:

string[] names = 
{
    "Jordan Ghassari",
    "James Cunningham",
    "Ghabriel Bercholee",
    "O^Brian",
    "Depto #345",
    "This is Ex$ample",
    "$amuel"
};

string searchFor = Console.ReadLine(); // Input
searchFor = @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))" + Regex.Escape(searchFor); // searchFor is input value to look for

Regex regEx = new Regex(searchFor, RegexOptions.IgnoreCase);

List<string> matchedNames = new List<string>();
foreach(string name in names){
    if (regEx.IsMatch(name))
    {
        matchedNames.Add(name);
    }
}

foreach (string match in matchedNames) 
{
    Console.WriteLine(match);
}

This is a tested and working solution. You just escape the portion of the pattern that the user inputs and then use the pattern to create a new Regex object. The \b can't be used to match special charecters so we use some C# lookbehinds as shown here. Then you loop though each of your strings and store the matches in some kind of data structure, I chose a generic list.

Upvotes: 1

Icemanind
Icemanind

Reputation: 48686

It would probably be simpler to do this without RegEx:

string searchString = "This is Ex$ample";
string searchFor = "Ex$a";

searchString = " " + searchString;
searchFor = " " + searchFor;

if (searchString.IndexOf(searchFor) >= 0)
{
    // Match
} else {
    // No Match
}

If you want a case insensitive search, use this instead:

if (searchString.IndexOf(searchFor, StringComparison.OrdinalIgnoreCase) >= 0)
{
    // Match
} else {
    // No Match
}

Upvotes: 0

Related Questions