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

My application has a function that parses text using a regular expression to extract special values. I also need to create lines that follow in the same format. Is there a way to use an already defined regular expression to create these lines?

For example, suppose my regex looks something like this:

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

I would like to use MyRegex to create a new line, for example:

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

Thus, created will have the value "sometextdata1".

Update . Judging by some of the answers below, I did not 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 above example, I provided "data1" to populate "group1". Basically, I have a regular expression that I want to use in a way similar to format strings, instead of also defining a separate format string.

+8
c # regex
source share
3 answers

You will need a Rex tool. Well, you don't need this, but this is what I use :-)

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

You can (though not perfect) add exe as a reference to your project and use the classes that have been published.

It works very well.

+3
source share

Native RegEx cannot do something like this.

If you really wanted to generate strings that met a set of criteria, you could examine specific sentence grammatical expressions (DCGs) instead of regular expressions. A logical programming language such as Prolog should be capable of generating strings that match your grammar rules.

From Wikipedia:

A basic DCG example helps illustrate what they are and how they look.

sentence โ†’ noun_phrase, verb_phrase.

noun_phrase โ†’ det, noun.

verb_phrase โ†’ verb, noun_phrase.

det โ†’ [the].

det โ†’ [a].

noun โ†’ [cat].

noun โ†’ [bat].

the verb โ†’ [eats].

It generates sentences such as โ€œcat eats a batโ€, โ€œbat eats a catโ€. You can generate all valid expressions in the language generated by this grammar in the Prolog interpreter ...

From your question, it seems this is not what you want to do. My advice would be to simply create a class that contained your Dictionary<String, String> object, and had its own ToString() method, which returned data in the appropriate format. That would be a lot easier :-)

eg:.

 public class SpecialObject { public Dictionary<string, string> SpecialDictionary { get; set; } public override string ToString() { return "sometext_group1data1"; // or whatever you want } } 
+1
source share

You might want to check if Pex can figure it out. Create a method that takes a string and returns the match of this regular expression. Pex may just be smart enough to find the source data for your method, which will test various aspects of the expression. (It may even help you catch some corner cases that you have not considered.

Other than that, no. You ask the system (regex) to make sure that it was not completely built.

0
source share

All Articles