Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a string that matches a given C# regex?

Tags:

c#

regex

My application has a feature that parses text using a regular expression to extract special values. I find myself also needing to create strings that follow the same format. Is there a way to use the already defined regular expression to create those strings?

For example, assume my regex looks something like this:

public static Regex MyRegex = new Regex( @"sometext_(?<group1>\d*)" );

I'd like to be able to use MyRegex to create a new string, something like:

var created = MyRegex.ToString( new Dictionary<string, string>() {{ "group1", "data1" }};

Such that created would then have the value "sometextdata1".

Update: Judging from some of the answers below, I didn't make myself clear enough. I don't want to generate random strings matching the criteria, I want to be able to create specific strings matching the criteria. In the example above, I provided "data1" to fill "group1". Basically, I have a regex that I want to use in a manner similar to format strings instead of also defining a separate format string.

like image 590
Chris Phillips Avatar asked Mar 10 '11 00:03

Chris Phillips


2 Answers

You'll need a tool called Rex. Well you don't 'need' it, but it's what I use :-)

http://research.microsoft.com/en-us/projects/rex/

You can (although not ideal), add the exe as a reference to your project and utilize the classes that have been made public.

It works quite well.

like image 174
Razor Avatar answered Oct 20 '22 09:10

Razor


Native RegEx cannot do something like this.

If you really wanted to generate strings that matched a set of criteria, you could investigate definite clause grammars (DCG) instead of a regular expression. A logic programming language such as Prolog should be able to generate strings that matched the grammatical rules you defined.

From Wikipedia:

A basic example of DCGs helps to illustrate what they are and what they look like.

sentence --> noun_phrase, verb_phrase.

noun_phrase --> det, noun.

verb_phrase --> verb, noun_phrase.

det --> [the].

det --> [a].

noun --> [cat].

noun --> [bat].

verb --> [eats].

This generates sentences such as "the cat eats the bat", "a bat eats the cat". One can generate all of the valid expressions in the language generated by this grammar at a Prolog interpreter...

From your question, it sounds like this isn't really what you want to do. My advice would to be to simply create a class that held your Dictionary<String, String> object and had a custom ToString() method that returned data in the appropriate format. This would be much easier ;-)

e.g.:

public class SpecialObject
{
    public Dictionary<string, string> SpecialDictionary { get; set; }

    public override string ToString()
    {
        return "sometext_group1data1"; // or whatever you want
    }
}
like image 39
Pandincus Avatar answered Oct 20 '22 08:10

Pandincus