Verbatim String or Regex Pattern from Input

A

Aaron

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
 
J

Jon Skeet [C# MVP]

Aaron said:
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?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
A

Aaron

Jon Skeet said:
"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.Readline();
if(Regex.Match("12", pattern).Success)
{
Console.WriteLine("match");
}
else
{
Console.WriteLine("no match");
}

The output will be: no match.

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

Regards,

Aaron
 
J

Jon Skeet [C# MVP]

Aaron said:
An example of the problem would be:

That's an example of the *problem*. Complete code would be useful.
You've provided *some* code, but it's not complete and it doesn't quite
compile.
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.Readline();
if(Regex.Match("12", pattern).Success)
{
Console.WriteLine("match");
}
else
{
Console.WriteLine("no match");
}

The output will be: no match.

Try this code, which is like the above but complete and compiles:

using System;
using System.Text.RegularExpressions;

public class Test
{
static void Main()
{
string pattern = Console.ReadLine();
if(Regex.Match("12", pattern).Success)
{
Console.WriteLine("match");
}
else
{
Console.WriteLine("no match");
}
}
}

When I run it, and type in \d+ it prints out "match" as expected.

Please provide a complete program like the above which *doesn't* work.
 
G

Gilles Kohl [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top