简体   繁体   中英

C# Regex Exception - insufficient hexadecimal digits

I am trying to write some regex pattern code that will take out certain parts of a text by using groups. Here is the code:

    private static string ParseSnippet(string ownitDirectory, string project) 
    {
        Match m = Regex.Match("(" + project + "\\\\.*\\\\)", ownitDirectory, RegexOptions.IgnoreCase);
        return m.Groups[1].Value;
    }

The problem I am having is that the pattern I am using causes an exception to occur. This exception does not make any sense to me. I have tested the pattern in plenty of online testers but this did not help the problem.

Would anybody have any suggestions as to what I should do?

EDIT:

Forgot to mention that the exception is "Insufficient hexadecimal digits"

tldr: You most likely got the error because your input has "\\x" or "\\u\u0026quot; in it, such as the directory "c:\\xenophobe" or "c:\\user..."

According to this page ( http://www.regular-expressions.info/quickstart.html ), "\\x" is used to indicate Unicode or other language set characters. For Unicode, both "\\x{FFFF}" or "\￿" match a Unicode character with the given hexadecimal index. Since the name of your folder after the x/u doesn't match the 0-9, af pattern of hex numbers and your folder is being used as the regex match, you were getting an error. Anytime you use your string to be compared as the regex accidentally, you're bound to get regex parsing problems.

According to this page , it seems that the parameters for Regex.Match are inverted. Have you tried this?

Regex.Match(ownitDirectory, "(" + project + "\\\\.*\\\\)", RegexOptions.IgnoreCase);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM