Textpad Regular Expressions

TextPad is a great editor, been using it for years, but never really used it to it’s full potential. Recently I needed to do some find/replace work in some huge SQL and CSV files – huge enough to make manual editing impossible – so I had to start using Textpad’s Regular Expression capabilities.

Here are a few expressions that came in handy. I intend to add to this list as time goes on.
Regexp shown in Green, my comments in Red:

CSV Editing:

Remove spaces and tab characters at the beginning of a line:
Find what: <span style="color: #008000;">^[ t]+</span>
Replace With: <span style="color: #ff0000;">don't enter anything in the field</span>

Remove spaces and tab characters at the end of a line:
Find what: <span style="color: #008000;">[ t]+$</span>
Replace With: <span style="color: #ff0000;">don't enter anything in the field</span>

Add “Hello” to the beginning of every line:
Find what: <span style="color: #008000;">(^.*)</span>
Replace With: <span style="color: #008000;">Hello 1</span>

Add “Hello ” to the beginning and ” World” to the end of every line:
Find what: <span style="color: #008000;">(^.*)</span>
Replace With: <span style="color: #008000;">Hello 1 World</span> <span style="color: #ff0000;">(watch the spaces)</span>

Find empty fields (i.e. “, ,”) with spaces or tabs in, and replace with empty field (“,,”):
Find what: <span style="color: #008000;">,[ t*],</span>
Replace With: <span style="color: #008000;">,,</span> <span style="color: #ff0000;">(just that, nothing else, just 2 commas)</span>

Remove blank lines in a text file, by searching for two linebreaks next to each other, and replacing with one:
Find what: <span style="color: #008000;">nn</span>
Replace With: <span style="color: #008000;">n</span>

Replacement Expressions:

Extract email addresses only from the following text: “Joe Blogs (job.blogs@blogsworld.com)”
This expression searches for 2 tagged expressions, firstly any printable characters including spaces up to the first open bracket symbol, secondly anything between the brackets. It then replaces the whole line with the second match.
Find what: <span style="color: #008000;">^([[:graph:] ]+)(([[:graph:] ]+))</span>
Replace With: <span style="color: #008000;">2</span>

Submitted by Paul: “Here’s how to convert csv to xml with a (quite large) regexp you can alter/extend for your needs.”
Find what: <span style="color: #008000;">^"([[:print:]]+)","([[:print:]]+)","([[:print:]]+)"</span>
Replace With: <span style="color: #008000;">123</span>

Notes:

  • The expressions above need the ‘Regular Expression’ condition to be ticked in the Find or Replace dialogue boxes for them to work.
  • Text pad needs to be set to use UNIX not POSIX style expressions. To set this, open ‘Configure > Preferences’ (Ctrl+Q,P), find the ‘Editor’ settings, and untick the ‘Use POSIX regular expression syntax’ box.

Please let me know if this post was useful with a click!
Nope, not helpful...Yep, more useful than not! - +27 thumb, 29 overall.
Loading...

7 thoughts on “Textpad Regular Expressions”

  1. Thanks. This was really helpful.

    I’ve been using TextPad for everything — with the exception of exploding X12 files. The files are one really long line with segments separated by tildes (~). I tried doing a find/replace on “~” and replacing them with a new line character “n” but TextPad would always put the actual text “n” instead of breaking the line. In these cases I would always open the file in Word and use their find/replace to replace the tildes with new paragraphs “^p” and then copy the result back into TextPad.

    Now I know that in TextPad all I have to do for this to work is to check the “Regular Expression” box and it works!

    Thanks a bunch!!

  2. Textpad help, To make a long story short, do you know how find say a number sequence and replace with additional text without changing the number you used to search on it the first place?

    find: [0-9]n$
    replace with: keep the same only add something unique to the ending [same as before]~n

    1. Hi Matt,

      If you just want to add “something” and then a line-break to ANY line you could use this:

      find: (^.*)

      replace: 1something

      It’s the use of parenthesis that allow the ‘replacement expression’ to work.

  3. Great stuff here Matt – thank you. The one I always need and can;t figure out is how to replace a TAP with the equal amount of white space with actual spaces. Any Ideas?

  4. How about this one guys and gals:

    You have a string “abcdef” throughout a textpad document.

    But you ONLY want to find the “abcdef” that is at the BEGINNING of a line.

    From the above, it looks like

    Find what: (^.abcdef)
    Replace With: “abcdef 1

    The difference being that you add a quote ” at the begining of the line. I only want to do that if it is at the begging of the line.

    Thanks,
    -Chris

    Edit: And I have POSIX turned off.

  5. TextPad:
    I want to replace newline \n with \n
    but get: (newline)

    Use to work fine before update to 8.1.2(64 bit)

Leave a Reply

Your email address will not be published. Required fields are marked *