Notepad++ Regular expressions

Notepad++ regular expressions

For those of you that regularly use notepad++, you would have come across the "Regular expression" option on the Replace window. I'm sure the code will work for older versions, but I've tested and been using these against version 6.9.

As a developer, I've used this option to replace elements in my text files (example: replacing invalid date delimiter characters). Let me give you some tips on finding and replacing characters in files needed for processing:

Find what(\d{4}+)\.+(\d{2}+)\.(\d{2}+)
Replace with: \1-\2-\3
Synopsis: We wish to search for a number (e.g. a date in our case), which starts off with 4 digits; followed by a decimal point; followed by 2 digits; followed by a decimal point; followed by 2 digits. We then wish to replace what was found with these these groups of numbers, separating them with a hyphen character.

The plus + symbol after each character group ensures the separate groups are looked at as a single string to find. Without the plus, the find will land on any number that at least satisfies on of the digit groups we wish to find - which is not what we want. We strictly want to land on date strings.

Find what(\d{4}+)\.+(\d{2}+)\.(\d{2}+)
Replace with: \1-\3-\2
Synopsis: This is an illustration of changing the order of the character groups. In our example, we are swapping the order of the digits - the month and day part of the date is swapped.

Combining Text Search with Regular Expressions:

Lets say, we wish to find a combination of standard text strings combined with a regex search. Lets say we want to replace a string like: ID VARCHAR(67) NULL,

In the string above, want to remove all the text after the "ID" column name, and replace it with a "," (i.e. comma):

Find what\s+VARCHAR\(+\d{1,3}+\)+\s+NULL,
Replace with: ,
Synopsis: We wish to search for space character followed by the word VARCHAR followed by the actual opening round bracket, followed by a sequence of digits of between 1 and 3 in length. Next we require a closing round bracket, followed by a space character and the word NULL

Finding number words at beginning of line

Lets say we want to search a file for lines that start with a numeric word: ie. 8786822|TY|TRUE|223

Find what\r\n(\d{1,14})
Replace with:
Synopsis: We wish to search for lines beginning with a numeric character word.



Happy Regex peeps.
www.silvafox.co.za

Comments

Post a Comment

Popular posts from this blog

Moving Data [Spark Streaming - simple data files]

RSS feeds to your webpage [part 1]