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: ^[ \t]+
Replace With: don't enter anything in the field
Remove spaces and tab characters at the end of a line:
Find what: [ \t]+$
Replace With: don't enter anything in the field
Add “Hello” to the beginning of every line:
Find what: \(^.*\)
Replace With: Hello \1
Add “Hello ” to the beginning and ” World” to the end of every line:
Find what: \(^.*\)
Replace With: Hello \1 World (watch the spaces)
Find empty fields (i.e. “, ,”) with spaces or tabs in, and replace with empty field (“,,”):
Find what: ,[ \t*],
Replace With: ,, (just that, nothing else, just 2 commas)
Remove blank lines in a text file, by searching for two linebreaks next to each other, and replacing with one:
Find what: \n\n
Replace With: \n
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: ^\([[:graph:] ]+\)(\([[:graph:] ]+\))
Replace With: \2
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: ^"\([[:print:]]+\)","\([[:print:]]+\)","\([[:print:]]+\)"
Replace With: \1\2\3
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.
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!!
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
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.