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.
c # regex
Chris phillips
source share