buy viagra
Home » Textpad, regular expressions

Textpad Regular Expressions

10 December 2008 12 Comments

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

 

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.

12 Comments »

  • Shailesh (author) said:

    All above exampls are useful.
    examples are always better than percept. I wish there would be more of examples.

  • Nico said:

    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!!

  • matt said:

    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

  • admin said:

    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.

  • Waldemar said:

    Hi Ben,

    Thanks for posting about the Textpad Regular Expressions.

    I have a question.
    In a txt-file I’m trying to remove all text between curly brackets including the curly brackets themselves.

    I can’t seem to get the regular expression right.

    Could you give me some help?

    Much obliged,

    Waldemar

  • Paul said:

    Here’s how to convert csv to xml with a (quite large) regexp you can alter/extend for your needs.

    Regexp:
    ^"\([[:print:]]+\)","\([[:print:]]+\)","\([[:print:]]+\)"

    replace with:
    \1\2\3

    Cheers

    Paul

  • matelot said:

    thanks !
    “Remove spaces and tab characters at the end of a line” is what I’m looking for.

  • tony said:

    YOU ROCK!
    Thank you so much for the “Add “Hello ” to the beginning and ” World” to the end of every line” example.

  • Adnan said:

    Hi,i want to replace two tags from one tag. i want to see both replacing tags in separate lines, not in same line.
    Any reply will be appreciating. Thanks

    Example:

    “separate lines”
    by

  • Graham Wells said:

    just got a quick question..I need to list down email address in a messy text..can anyone say how to write rg expression for that??

  • Yves said:

    Hello,
    I have a huge file (50000 lines) that comes from a translation memory application. The export went well but there was on glitch with every single title in the doc. There is no space between the bullet-numbers (or numbers+dot) and the text.
    Here is how it looks:
    3.1.International Organizations
    3.1.1.World Bank
    OR
    3.1International Organizations
    3.1.1World Bank
    How would I insert space between a digit (or a digit+dot) and the alpha character while maitining the corresponding digit (or digit+dot) and character.
    I would like to achieve this
    3.1. International Organizations
    3.1 International Organizations
    Thank you kindly,
    Yves (New Caledonia)

  • Jim said:

    I have a file with lines that look like the following:

    KFY Korn Ferry International Q4 2010 $ 0.13 14-Jun AMC
    LAKE Lakeland Industries Q1 2011 n/a 14-Jun AMC
    LZB La-Z-Boy Inc. Q4 2010 $ 0.23 14-Jun AMC
    373400.T M.P. TECHNOLOGIES INC TOKYO Q3 2010

    I would like to replace each line with the first “word” of that line provided that that word does not begin with a digit. The resulting lines of text would be:

    KFY
    LAKE
    LZB

    What would the regex look like? Any help would be appreciate. Thanks!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.