Verbatim String or Regex Pattern from Input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QWFyb24=?=

    Verbatim String or Regex Pattern from Input

    Hi,

    I'm having a tricky problem where I want to accept a regular expression
    pattern from user input but can't get teh escape characters to be prcoessed
    correctly. If I take the same pattern and declare it in code with a
    preceeding @ character it works fine.

    To get the pattern to work from teh suer all \ have to be escaped, e.g.
    instead of \d a user would have to enter \\d.

    Is there anyway to convert a normal string to a verbatim string or some
    other way to get a string treated as a verbatim string by Regex?

    Regards,

    Aaron
  • =?Utf-8?B?QWFyb24=?=

    #2
    Re: Verbatim String or Regex Pattern from Input



    "Jon Skeet [C# MVP]" wrote:
    Aaron <Aaron@discussi ons.microsoft.c omwrote:
    I'm having a tricky problem where I want to accept a regular expression
    pattern from user input but can't get teh escape characters to be prcoessed
    correctly. If I take the same pattern and declare it in code with a
    preceeding @ character it works fine.

    To get the pattern to work from teh suer all \ have to be escaped, e.g.
    instead of \d a user would have to enter \\d.

    Is there anyway to convert a normal string to a verbatim string or some
    other way to get a string treated as a verbatim string by Regex?
    >
    "Verbatim string literal" is a strictly compile-time business - it's
    only relevant in actual C# code.
    >
    Could you post a short but complete program which demonstrates the
    problem?
    >
    An example of the problem would be:

    1. Ask the user for some input to be used as a regular expression pattern:

    Enter =\d+

    2. Read it in and attempt to use it as a regex pattern.

    string pattern = Console.Readlin e();
    if(Regex.Match( "12", pattern).Succes s)
    {
    Console.WriteLi ne("match");
    }
    else
    {
    Console.WriteLi ne("no match");
    }

    The output will be: no match.

    3. If the user enters \\d+ the output will be: match.

    Regards,

    Aaron

    Comment

    • Gilles Kohl [MVP]

      #3
      Re: Verbatim String or Regex Pattern from Input

      On Tue, 1 Apr 2008 03:08:07 -0700, Aaron <Aaron@discussi ons.microsoft.c om>
      wrote:
      >Hi,
      >
      >I'm having a tricky problem where I want to accept a regular expression
      >pattern from user input but can't get teh escape characters to be prcoessed
      >correctly. If I take the same pattern and declare it in code with a
      >preceeding @ character it works fine.
      >
      >To get the pattern to work from teh suer all \ have to be escaped, e.g.
      >instead of \d a user would have to enter \\d.
      >
      >Is there anyway to convert a normal string to a verbatim string or some
      >other way to get a string treated as a verbatim string by Regex?
      >
      >Regards,
      >
      >Aaron
      Hmm, is Regex.Escape what you are looking for?

      Regards,
      Gilles.

      Comment

      Working...