Home Fabio Cortesi Stefan Jäger

Welcome to the Joint-Venture Blog from Fabio Cortesi and Stefan Jäger.

 

 
 

 

As mentioned on the Programmer’s Notepad help site (http://pnotepad.org/docs/RegularExpressions), Regular Expressions are supported. But what are the benefit of Regular Expressions? In this blog entry, I will show two examples, how to use Regular Expression in Programmer’s Notepad.

If you have to replace a specific word (e.g. %test%) with another word (e.g. Hello), Regex doesn’t really make sense. Just use the regular search & replace window.
image

But what, if you have several entries like ?1, ?2, ?3, … and you want to replace them with {1}, {2}, {3}, …? With the regular search & replace window, you have to replace ?1 with {1}, ?2 with {2}, and so on. Furthermore, there are some problems with ?10, which gets replaced with {1}0 if you start replacing ?1 with {1}. In this situation, Regular Expressions is the solution.

Regular Expression always works char by char (not by a word). Inside the brackets [], there can be defined, which chars are allowed, e.g. [0-9] means, the numbers 0,1,2,3,4,5,6,7,8,9 are allowed. To select a year, just write [0-9][0-9][0-9][0-9]. To select any number, just write [0-9]+. The + means, at least one ore more occurrences.

Our search pattern for the previous described example should search for a question mark, followed by numbers. There can be a single number ore more numbers. Last but not least, the number should be extracted and surrounded by {} in the replacement.

  1. ? could also be a Regular Expression (zero or one occurrences, unfortunately not supported by Programmer’s Notepad), so we have to escape it with a backslash. –> \?
  2. The following number(s) should be extracted and surrounded with (). In the Programmer’s Notepad syntax, you have to use \(\) to declare, that something should be extracted. Everything found between this to brackets are later accessible with \1, \2, …
  3. We allow any number from 0 to 9 –> [0-9]
  4. This number appears at least once or more –> [0-9]+
  5. Combined, the search pattern is \?\([0-9]+\)
  6. The replace pattern is simpler. The expression found between the \(\) get’s stored into \1. Now use this placeholder between the curly brackets {\1}.

image 

This replaces following examples with just one click. It’s easy, not?
?1 –> {1}
?10 –> {10}
?2 –> {2}
?3 –> {3}

 

The second example is a little bit more advanced. I got the result of a SQL query, separated by spaces, which I needed surround with some other words and in another order. The SQL result, which I had, was something like:
1       Monday      9       No description
1       Monday      3       Fish and meat
1       Monday      4       Vegetables and fruits
2       Thusday     10      No details

As result, I wanted:
Day “Monday” (id 1), Desc “No description” (id 9)
Day “Monday” (id 1), Desc “Fish and meat” (id 3)
Day “Monday” (id 1), Desc “Vegetables and fruits” (id 4)
Day “Thusday” (id 2), Desc “No details” (id 10)

  1. First, there is a number (once or more times), which should also be extracted –> \([0-9]+\)
  2. After that, there are some spaces, but they should not be extracted –> [ ]+
  3. Followed by a text (to be extracted) –> \([A-Za-z]+\)
  4. Again some spaces –> [ ]+
  5. Again some numbers –> \([0-9]+\)
  6. At the end text with spaces –> \([A-Za-z ]+\)
  7. The result is the string \([0-9]+\)[ ]+\([A-Za-z]+\)[ ]+\([0-9]+\)[ ]+\([A-Za-z ]+\)
  8. this should be replaced into Day “\2″ (id \1), Desc “\4″ (id \3)

image

That’s working. Easy, heh?

Didn’t understand any word about Regular Expressions? Check out http://pnotepad.org/docs/RegularExpressions or http://www.regular-expressions.info/.

 

 

 

 

 
  1. Hi Steve, the next version of Programmer’s Notepad includes a full-featured regular expression engine for text searching so you’ll get the following:

    Support for ?
    Counted matches ([0-9]{4} for year)
    Proper group/capture syntax, \([a-z]\) becomes ([a-z])
    Back references
    … everything else you’d expect.

    You can try this code now by downloading the first 2.0.9 release at http://pnotepad.googlecode.com/

    Comment by Simon — 28.4.2008 @ 11:07

  2. Just tried the new version. It’s really good. \d or \w is supported and to use of brackets without the backslashes makes it really simple to read. Just how Regular Expression should be ;-) Thanks!

    Comment by Steve J. — 28.4.2008 @ 12:28

Leave a comment