1

I am trying to create a regex that will allow people to search with "*" as a wildcard, which from my understanding is pretty standard. I have the following for a current regex:

@"($([a-zA-Z0-9!\@#\$%\^&\(\)-_\+\.'`~/=\?\{\}\|]){6,255}|([\w]){0,0})"

When I run the code, and I try to do the search with the wildcard, it tells me "at least one character is required" which leads me to believe that it's only reading the wildcard part and not the first part that has all the characters in it. What am I doing wrong in this regex?

By the way, this is for an ASP.NET MVC 3 project in C#.

Edit::: This regex is part of the ViewModel in an MVC 3 project, the regex is part of the data annotations on a property in the viewmodel. I'm not looking to put code anywhere else but in the data annotation, which is why it's a single line of code. This is the full annotation for the code above:

[RegularExpression(@"($([a-zA-Z0-9!\@#\$%\^&\(\)-_\+\.'`~/=\?\{\}\|]){6,255}|([\w]){0,0})", ErrorMessageResourceType = typeof(AdminResource), ErrorMessageResourceName = "SearchWildcardError")]
tereško
  • 58,060
  • 25
  • 98
  • 150
Skrubb
  • 531
  • 1
  • 6
  • 25

3 Answers3

2

The problem you will find is that regular expressions are much more flexible than wildcards by nature, and therefore not as simple as a singular * meaning "any characters of any length".

In regular expressions the closest that you have to a wildcard is ., but if you do not quantify the length, it will always match only one instance of any character.

There are obviously a fair few ways that you can quantify regex .... .{3}, .*, .+ or even .++

The simplest way to replicate a wildcard is with .+ since it asserts that you can find anything between 1 and unlimited times. If you want to make it optional, then you would use .*

If you want the traditional type of wildcard, then regex is not the tool for it.

Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
0

This seems quite complicated, if I understand you correctly then you want to implement a wildcard search and use Regex underneath.

So if the user types hel*oo

Things like helcopteroo will match or just heloo

So I would do something like:

Regex reg = new Regex(@"hel[a-zA-Z0-9]*oo", RegexOptions.IgnoreCase);
var matches = reg.Matches("helloo hel022oo helcoopter");

You would just need to generate the search pattern passed to the Regex constructor based on your input

Jaycee
  • 3,098
  • 22
  • 31
0

So, I found the answer by taking a little bit closer of a look at my code. I ended up breaking it out into 2 different regex's. One for username:

@"($(\w@\.){6,255}|([a-zA-Z0-9!\@#\$%\^&\(\)-_\+\.'`~\/=\?\{\}\|]){2,50}|([\w]){0,0})"

That still allows normal searching and using the wildcard with all the special characters I need. The error on this one was not escaping the "/" properly. This one is working fine now. The second regex used for the email one:

@"($(\w@\.){1,50}|([a-zA-Z0-9@\*\._]){2,50}|([\w]){0,0})"

Only allows a few special characters, and still allows searching by wildcards, but MUST include the "@" sign to make it a valid search.

P.S. I found a nifty link that allows you to test regular expressions that works dandy, if anyone needs it, you can find it here: http://regex101.com/#pcre This little tool works great!

Skrubb
  • 531
  • 1
  • 6
  • 25