8.3. Answers (For Regular Expressions)

8.3.1. Scenario: dd/mm/yyyymm/dd/yyyy

If old text is 30/01/2007 it will be replaced with 01/30/2007

In visual studio, find the pattern: ([0-9]{2})/([0-9]{2})/([0-9]{4}). Ensure you click on .* to enable search with regular expressions.

Then, replace with $2/$1/$3

In other some edits, the replace pattern might be required to be \2/\1/\3.

Let’s understand the regular expression.

For the search section:

  • [0-9] - Match number from 0-9

  • [0-9]{2} - Match number from 0-9, and same thing repeated twice.

  • ([0-9]{2}) - Using the round brackets, we make this as a Group

  • In the above example, we have 3 groups.

  • .* is required to select the search to be regular expression. Else it would be plain text search.

For the replace section:

  • Here, array starts with 1. :-)

  • Since we are swapping dd/mm/yyyy into mm/dd/yyyy, i.e. 1-2-3 into 2-1-3, the replace pattern is $2/$1/$3

  • $ for replace means selector for the matched group.

  • Note: In case of search section, $ denotes end of line.

8.3.2. Scenario: Hexadecimal Words

# download a list of common words from internet to a local file.

wget https://www.mit.edu/\~ecprice/wordlist.10000 -O dict_list.txt


# E-Grep. Find using regular expressions

egrep -i "^[a-f0-9]+$" dict_list.txt

Let’s understand the regular expression.

  • ^ - Match from the beginning of the line

  • [a-f0-9] - From a-f and 0-9, inclusive.

  • [a-f0-9]+ - From a-f and 0-9, inclusive. 1 or more such matches.

  • $ - Match till the end of the line

8.3.3. Scenario: Convert a sequence of numbers to hex for C Array

Assuming values are 223344556677, it can be replaced to 0x22, 0x33, 0x44, 0x55, 0x66, 0x77

Set the regular expression search pattern to ([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])

Set the replace pattern to 0x$1, 0x$2,.

If the file has other numbers, do not do find and replace all. Instead do find and replace in selection.

8.3.4. More Learning

https://regex101.com/ is an excellent site to learn more about it.