Tutorial #2: Replace a specific string in Word using wildcards

A recent task I had to perform was to remove a list of item numbers from a document while keeping the item name intact. The syntax was <ITMxxx – Item Name> which was required to be changed to only <Item Name>. The task seemed straight forward up front but the tricky part was that the spacing around the hyphen was inconsistent and so was the usage of a dash instead of a hyphen. The various forms taken by this element were:

<ITM123-Item Name>
<ITM345 -Item Name>
<ITM546- Item Name>
<ITM729 - Item Name>
<ITM189 – Item Name>

The idea was to replace all of them at one go while ensuring that nothing else was inadvertently replaced. Thus, the use of only the common wildcard character ‘?’ wouldn't have sufficed. Fortunately, Word provides a very powerful set of wildcards detailed at the following link:

To illustrate my case, the following strings in ‘Find What’ and ‘Replace With’ did the trick:
Find what: (ITM[0-9]{3}*[-–]*)(<*>)
Replace with: \2

[0-9] captures the presence of any number while {3} ensures that the search is limited to 3 occurrences of these digits thereby capturing the ‘ITMxxx’ part.

The part *[-–]* captures the presence of space preceding or following the hyphen or dash.

The round brackets () represent a complete search expression. Thus, in this case the search is split in to two expressions, the first being the entire text leading up to the ‘Item Name’ and the second being the first word of the item name. The Replace With string ensures that these two expressions are replaced by the second expression alone which happens to be the first word of the item name. The rest of the text is kept as-is thereby producing the requisite result.