RegEx -- Quantifier {x,y} following nothing error

yeahumok picture yeahumok · Jun 28, 2010 · Viewed 14k times · Source

I am very new to RegEx -- so can someone please help me figure out what exactly is going wrong here?

I have this code:

       string regPattern = "*[~#%&*{}/<>?|\"-]+*";
       string replacement = "";
       Regex regExPattern = new Regex(regPattern);

Yet, when my app hits the regExPattern line, i get an ArgumentException -- Quantifier {x,y} following nothing error.

Can someone help?

EDIT: I need to pass this pattern into a foreach loop like so:

    if (paths.Contains(regPattern))
        {
            foreach (string files2 in paths)
            {
                try
                {
                    string filenameOnly = Path.GetFileName(files2);
                    string pathOnly = Path.GetDirectoryName(files2);
                    string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
                    string sanitized = Path.Combine(pathOnly, sanitizedFileName);
                    //write to streamwriter
                    System.IO.File.Move(files2, sanitized);

                }
                catch (Exception ex)
                {
                    //write to streamwriter

                }
            }
        } 
        else
        { 
        //write to streamwriter

        }

How do i define the pattern if it is being passed into this loop?

Answer

Mark Byers picture Mark Byers · Jun 28, 2010

Update: after reading the comment to the question I think you want simply this:

s = Regex.Replace(s, "[~#%&*{}/<>?|\"-]+", "");

Old answer: I guess when you write * you are thinking of wildcards such as those you would enter at a shell:

*.txt

This is not how the * works in regular expression syntax. What you probably want instead is .*:

".*[~#%&*{}/<>?|\"-]+.*"

The . means "any character" and the * means "zero or more of the previous".

Inside the character class [...] the * loses its special meaning and becomes a literal character so it does not need to be escaped. Escaping it unnecessarily inside the character class will not cause any harm and some people find it easier to read.