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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With