Is there a way to create a string that matches a given C# regex?
My application has a feature that parses text using a regular expression to extract special values. I find myself also needing to crea开发者_JAVA技巧te 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.
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.
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
}
}
You might want to check to see if Pex can figure it out. Create a method that takes a string and returns whether that Regex matches it. Pex might just be smart enough to find inputs for your method that will test various aspects of the expression. (It might even help you catch some corner cases you hadn't considered.
Other than that, no. You're asking a system (regex) to do something it was totally not built to do.
精彩评论