/ private static void InsertDocument(Node insertionDestination, Document docToInsert) / /// Inserts all the nodes of another document after a paragraph or table. Remove the paragraph with the matched text. Insert a document after the paragraph containing the matched text. Private class InsertDocumentAtReplaceHandler : IReplacingCallbackĭocument subDoc = new Document(MyDir + "Document.docx") Options.ReplacingCallback = new InsertDocumentAtReplaceHandler()
#REGEX FOR NUMBER RANGE HOW TO#
MLog.AppendLine($ "\"Shows how to insert an entire document's contents as a replacement of a match in a find-and-replace operation.ĭocument mainDoc = new Document(MyDir + "Document insertion destination.docx") ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args) / private class TextFindAndReplacementLogger : IReplacingCallback / /// Maintains a log of every text replacement done by a find-and-replace operation /// and notes the original matched text's value. "\"NYC\" converted to \"Washington\" 42 characters into a Run node.", logger.GetLog().Trim())
"Hope to see all our (Old value:\"NYC\") Washington-based customers at the opening!", doc.GetText().Trim()) Īssert.AreEqual( "\"New York City\" converted to \"Washington\" 20 characters into a Run node.\r\n" + TextFindAndReplacementLogger logger = new TextFindAndReplacementLogger() ĭoc.Range.Replace( new Regex( "New York City|NYC"), "Washington", options) Īssert.AreEqual( "Our new location in (Old value:\"New York City\") Washington is opening tomorrow. Set a callback that tracks any replacements that the "Replace" method will make. We can use a "FindReplaceOptions" object to modify the find-and-replace process.įindReplaceOptions options = new FindReplaceOptions() "Hope to see all our NYC-based customers at the opening!") If the Seed value would be e.g.DocumentBuilder builder = new DocumentBuilder(doc) īuilder.Writeln( "Our new location in New York City is opening tomorrow. For example, when the regular expression from the beginning of the article is used, and the Seed value is 0, the first few rows of generated data would be: For every Seed value, there is exactly one set of possible generated data. The important thing to note is that the generated set of values is defined by the Seed value. The Unique option ensures unique values will be generated. which will generate random US phone numbers: () = Operators that are inside the parentheses are grouped \ = Indicates that the character that follows is interpreted as is, instead of as a special character The last four-digit station number can use numbers in a range from 0-9.įollowing this rule, we will create a regular expression that will generate random US phone numbers using syntax elements used for regular expressions in data generation, from the list below:
#REGEX FOR NUMBER RANGE CODE#
The rule for the real US phone number is that the three-digit area codes first digit can be in a range from 2-9, this applies to a three-digit central office code too. The example of the US phone number syntax is below:Īrea code – central office code – station number The NANP is a system of numbering plan areas (NPA) using telephone numbers consisting of a three-digit area code, a three-digit central office code, and a four-digit station number. They are defined by the North American Numbering Plan (NANP). To explain this, first the syntax of US base phone numbers must be understood.
For our example we will use US based phone numbers, and show how they can be generated using the regular expression. One of the most used information is a phone number, and that is a good example of data that can be used to populate a database with. Using regular expressions to create realistic test data
They can be also used as a data generator, following the concept of reversed regular expressions, and provide randomized test data for use in test databases. A regular expression (abbreviated regex or regexp and sometimes called a rational expression) is a sequence of characters that forms a search pattern, mainly for use in pattern-matching and "search-and-replace" functions.