0

I couldn't figure this out after spending 2 days of Google search and trials.
Question: Is there any way to use a regular expression that includes \u or \x?

What I want to do: I am reading a long text in string, and I need to check if that text contains the following pattern:

\20\999\u[0-f]{4}

So for example, if the text includes \20\30\u001a, it needs to be true and I actually need to retrieve this value. (By the way, the real regular expression is way longer than this, but I shortened it for convenience.)

The code I have that is not working:

private string re = @"\20\999\u[0-9a-f]{4}";
private Match CheckIfMatches(string textToBeChecked)
{
    Regex regex = new Regex(this.re, RegexOptions.IgnoreCase);
    return regex.Match(textToBeChecked)
}

private void DoSomeOperation(string textToBeChecked)
{
    string s = "";
    try
    {
        Match matchResult = CheckIfMatches(textToBeChecked);
        if(result.Success)
        {
            s = result.Value;
            //Do more operations
        }
        else
        { 
            //Do more operations
        }
    }
    catch(Exception ex)
    {
         //Do error handling
    }
}

When I test-run this program with some text, it returns an error and it goes to the exception with Insufficient Hexadecimal Digits. I know this is happening because the regular expression contains \u. But I actually need to include this in the regular expression.

I tried removing @, and also adding one extra \ to see if it makes any difference, but it didn't.

How can I use a regular expression in C# with \u or \x?



Edit: Below is a part of the whole string stored in textToBeChecked as the entire string is too long, and also it is confidential.

Test sample test sample test sample \03\02\0\u0010\ test sample test sample

Something like above is stored in this variable. And the actual code I am using is

private string re = @"\\03\\02\\0\\u[0-9a-f]{4}";

result.Success in my code in the original question returns false to this.

J.Doe
  • 329
  • 3
  • 14
  • 5
    Maybe you want `@"\\20\\999\\u[0-9a-f]{4}"`? – Wiktor Stribiżew Oct 23 '19 at 19:16
  • Side note: " \20\30\u001a" *does not* match the pattern you are looking for. Please review [MCVE] guidance on posting code as it is not clear what you are getting wrong (in addition to not knowing how to create regex for "\"). – Alexei Levenkov Oct 23 '19 at 19:43
  • I cannot figure out why this does not match the pattern. I tried the code with the suggestion @WiktorStribiżew gave me. The error disappeared, but the match fails. – J.Doe Oct 23 '19 at 23:06
  • Add the exact string or, better, string literal with the text you need to handle, to the question. What is in `textToBeChecked`? – Wiktor Stribiżew Oct 23 '19 at 23:08
  • I edited my original question. @WiktorStribiżew – J.Doe Oct 23 '19 at 23:19
  • But you have ``\03\02\0\u0010\``. Try `Regex.Replace(textToBeChecked, @"\s*(?:\\\d+)+\\u[0-9a-f]{4}\\", "")` to remove the matches, see [this C# demo](https://ideone.com/E3C07K) – Wiktor Stribiżew Oct 23 '19 at 23:22
  • @WiktorStribiżew The replace method did not replace anything. It seems like after running this method, the stored text is still the same as the original. – J.Doe Oct 23 '19 at 23:31
  • Then you are mistaken about your *literal text*. LITERAL TEXT != STRING LITERAL. – Wiktor Stribiżew Oct 24 '19 at 06:48

0 Answers0