RegEx -- Quantifier {x,y} following nothing error
Asked Answered
A

5

6

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?

Argosy answered 28/6, 2010 at 19:40 Comment(1)
To be specific -- the pattern i have in the code is meant to get rid of those invalid characters in file names. therefore i need to get rid of an asterisk, tilde, pound sign, brackets, angle brackets, etc. is this the correct pattern for that?Argosy
R
7

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.

Robalo answered 28/6, 2010 at 19:41 Comment(3)
* means asterisk -- not any character. Do i still need to backslash it?Argosy
The meaning of * changes depending on whether it is inside a character class or not. Inside a character class it means a literal * whether or not it is escaped. Outside of a character class it means "zero or more" if unescaped and a literal * if escaped.Robalo
+1 (for the revision), and in C# you should use verbatim strings for regexes. They don't use the backslash as an escape character; you only have to escape the quotation mark with another quotation mark: @"[~#%&*{}/<>?|""-]+"Spirketing
T
2

The * is a quantifier meaning "zero or more times" (same as {0,}). You'll have to escape it using a backslash like this: \*

Tiny answered 28/6, 2010 at 19:43 Comment(0)
C
0

Since you're doing a Regex.Replace to replace any of these one-character matches with an empty string:

        string pattern = "[~#%&*{}/()<>?|\"\\\\-^[\\]]";

        string input = @"(*&af%\#$}afd]a#f%hjg{d(^(^[RF*()^FR(7r5";

        string output = Regex.Replace(input, pattern, String.Empty);
Concert answered 28/6, 2010 at 20:1 Comment(0)
A
0

Add . before the *

e.g. string regPattern = ".*[~#%&*{}/<>?|\"-]+.*";

Angeliqueangelis answered 3/6, 2014 at 13:46 Comment(0)
B
0

The wild card character * does not work fine alone here I tried it and to make it work fine, we need to add a . before * and wholly as .* this should work.

Breezeway answered 9/4, 2021 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.