Textpad and Finding Things
 A primer for the uninitiated


          Graham Lally
           April 2012

     graham.lally@ocsi.co.uk
      http://www.ocsi.co.uk/
Coming up

• Hello Textpad
• Learning to love Regular Expressions
• Other useful things
Why Textpad?



                       +
        Text-editing       Lots of options

            http://www.textpad.com/

                                   http://www.flickr.com/photos/eldholm/2354982554/
                              http://www.flickr.com/photos/grandmaitre/2387376806/
Here are the
files you
have open

               Write things here
Configure > Preferences
Basic set-up

         C   Allow multiple instances to run
         C   Replace Notepad
         C   Put shortcut to Textpad on: Context Menu
Also set under "Editor" settings:

         C   Use POSIX regular expression syntax

This means we can use ( ) and { } instead of ( ) and { }
Shortcuts to get started


   Search current file(s)             F5

   Search and replace                 F8

   Search through a whole directory   Ctrl + F5
Regular Expressions

Why (oh why)?

  Regular expressions (REs / RegExps) let you search for a
  *range* of things in one go, rather than just a single thing.

  Old: Find "Kings Cross"       RE: Find "King'?s (Cross|X)"

  Kings X                       Kings X
  King's X                      King's X
  Kings Cross                   Kings Cross
  King's Cross                  King's Cross
Why Regular Expressions?

Also find:

     Any letter/number

     Any letter followed by any number

     Any number at the start/end of a line

     Any non-numbers

     etc.
My first regular expression
                       F5:




             C   Regular expression

            Find: .a
".a"

"." is a special character in REs, meaning find any character
   (including letters, numbers, spaces and punctuation)

So "..." finds any 3 characters in a row: abc, 123, y r, even ...

             .a          Find any character followed
                          by an "a", e.g. ba, 1a, %a

And "a.c" will find abc, aac, a6c, a?c, a c, a[c, a-c, etc
Characters to watch out for

In Textpad*, most characters do what they should.

Some don't:

             .    ?       *   +   |   [   ]    (    )   {     }      ^    $

To actually look for one of these, put a "" before it. e.g. ? (

To look for a , use 

                                                            (* Other RegExp software may differ)
Finding other things

   [...]     Find any single character listed in the [ ]

             e.g. [abc][123] will match a1, c1, b2, c3, etc

   ...|...   Find anything that matches before
               or after the |

             e.g. abc|123 will match abc and 123

             Can also be more than 2: abc|123|xyz|000
Counting things

Use +, * or ? after a character to indicate how many times it
should occur:

       +          Find one or more characters
                   e.g. ab+c will find abc, abbc, abbbbbc, etc

       *          Find zero or more characters
                   e.g. ab*c will find ac, abc, abbc, abbbbbc, etc

       ?          Find zero or one characters (i.e. "maybe")
                  e.g. ab?c will find ac and abc, but not abbc
Counting things

Use {min,max} after a character to set your own limits

  e.g. ab{4,6}c will find abbbbc, abbbbbc and abbbbbbc

max is optional too

  e.g. ab{3,}c will find abbbc, abbbbbbbbbbbbbbbbbbc, etc

min isn't optional in Textpad, so use 0 instead

  e.g. ab{0,3}c will find ac, abc, abbc, abbc
More character ranges
Textpad also has some "named" ranges you can use, e.g.

    [:alpha:]        Any letter

    [:digit:]        Any digit

    [:blank:]        Space or tab

    [:space:]        Space, tab, return, line feed, et al

These go inside other [ ] e.g. [[:alpha:][:digit:]] finds any letter or
number.

See TextPad's Help (available from the Find dialogue) for
more.
Character ranges

Writing [0123456789] every time is silly.

Instead, use [...-...] (dash) to specify a range of characters.

  e.g. [0-9] to find any single number
       [a-z] to find any letter

These can be combined with themselves, plus other characters

  e.g. [a-z0-9:.] to find any letter, number, colon or period
Line and word endings
    n         Lets you look for line breaks,
               e.g. Hellonworld will find Hello
                                           world

    < and > Match the start and end of a word

    ^         Match the start of a line
              e.g. ^: will find any colon at the start of a line

    $          Match the end of a line
               e.g. .$ will find any period at the end of a line

Combine these to match whole lines, e.g. ^?.*$ to match all
lines that start with a question mark
Getting more complicated

Use (...) to set up "groups" - these can then be checked and
counted in themselves

e.g. (hello)+ will find hello, hellohello, hellohellohello, etc.

    (hello|bye){2} will find hellohello, byebye, hellobye and byehello

    Hello (Tom|Stefan|Emma) will look for any of the 3 names

    (t[io]ck){2}! will find ticktock!, tocktick!, ticktick!, and tocktock!
More examples to figure out

What do the following regular expressions do?

    ^[0-9]{3}

    ^(Source|[0-9]{3})

    ^.*[0-9]{3}$
Replacements

                             F8




As expected, Textpad will replace anything matched, with the
text specified.
Replacements

Remember though:

The RE will (only/completely) match what you've typed.
Textpad will replace what's been matched.

Work out what's being matched and what's being replaced.

e.g. We may want to   match (find) all lines ending with a ?, but
only replace the word before the ?
Replacements

A more useful example:

     [a-z]+[?!]$   Find any lines ending in letters, then ? or !

We want to replace the letters (i.e. [a-z]+) with something, but
keep the ? or ! in place

But we don't know which one to use when replacing...
Replacements

Luckily, we can refer to things that have been matched using
(...) groups.

Each group has its own number (in sequential order), and can
be inserted using n

e.g. [a-z]+([?!])$ has one (...) group: 1

     To replace the letters, we can use new word1

     So hello! would become new word!
Replacements

Textpad uses 1 to 9 - which we can use wherever we want

e.g. ([a-z]+) ([0-9]+) will match Pelham 123

     Replacing this with 2 1 gives us 123 Pelham

     Replacing it with 22 gives us 123123

(Be a bit careful if a (...) group is optional though, i.e. (...)? - if
it's not found, your higher numbers will be wrong.)
Phew...

Can get tricky quickly, but remember:

   o   Use the "Help" button

   o   Use the "Find Next" button to check what your RE finds
   o   Check the Cheat Sheet - http://bit.ly/textpadref
Handy Tip 1: Multiple files
To replace across many files, open all the files and tick "All
documents" when replacing:
Handy Tip 2: Searching a folder
To look through (closed) files in a directory, use Ctrl + F5. Tick
"Search subfolders" if folders are several layers deep.
Handy Tip 3: Save everything

Finally:

You can see which files have changed - these have a * next to
their filename.

You can also save all changes to all open files using:

                        File > Save All
Other Textpad Useful Bits
Block select mode (Ctrl+Q, then B):



                                       Bookmarked lines:




Cut/copy/delete
everything selected
or bookmarked.

                                Bookmarks
http://xkcd.com/208/

Textpad and Regular Expressions

  • 1.
    Textpad and FindingThings A primer for the uninitiated Graham Lally April 2012 graham.lally@ocsi.co.uk http://www.ocsi.co.uk/
  • 2.
    Coming up • HelloTextpad • Learning to love Regular Expressions • Other useful things
  • 3.
    Why Textpad? + Text-editing Lots of options http://www.textpad.com/ http://www.flickr.com/photos/eldholm/2354982554/ http://www.flickr.com/photos/grandmaitre/2387376806/
  • 5.
    Here are the filesyou have open Write things here
  • 6.
  • 7.
    Basic set-up C Allow multiple instances to run C Replace Notepad C Put shortcut to Textpad on: Context Menu Also set under "Editor" settings: C Use POSIX regular expression syntax This means we can use ( ) and { } instead of ( ) and { }
  • 8.
    Shortcuts to getstarted Search current file(s) F5 Search and replace F8 Search through a whole directory Ctrl + F5
  • 9.
    Regular Expressions Why (ohwhy)? Regular expressions (REs / RegExps) let you search for a *range* of things in one go, rather than just a single thing. Old: Find "Kings Cross" RE: Find "King'?s (Cross|X)" Kings X Kings X King's X King's X Kings Cross Kings Cross King's Cross King's Cross
  • 10.
    Why Regular Expressions? Alsofind: Any letter/number Any letter followed by any number Any number at the start/end of a line Any non-numbers etc.
  • 11.
    My first regularexpression F5: C Regular expression Find: .a
  • 12.
    ".a" "." is aspecial character in REs, meaning find any character (including letters, numbers, spaces and punctuation) So "..." finds any 3 characters in a row: abc, 123, y r, even ... .a Find any character followed by an "a", e.g. ba, 1a, %a And "a.c" will find abc, aac, a6c, a?c, a c, a[c, a-c, etc
  • 13.
    Characters to watchout for In Textpad*, most characters do what they should. Some don't: . ? * + | [ ] ( ) { } ^ $ To actually look for one of these, put a "" before it. e.g. ? ( To look for a , use (* Other RegExp software may differ)
  • 14.
    Finding other things [...] Find any single character listed in the [ ] e.g. [abc][123] will match a1, c1, b2, c3, etc ...|... Find anything that matches before or after the | e.g. abc|123 will match abc and 123 Can also be more than 2: abc|123|xyz|000
  • 15.
    Counting things Use +,* or ? after a character to indicate how many times it should occur: + Find one or more characters e.g. ab+c will find abc, abbc, abbbbbc, etc * Find zero or more characters e.g. ab*c will find ac, abc, abbc, abbbbbc, etc ? Find zero or one characters (i.e. "maybe") e.g. ab?c will find ac and abc, but not abbc
  • 16.
    Counting things Use {min,max}after a character to set your own limits e.g. ab{4,6}c will find abbbbc, abbbbbc and abbbbbbc max is optional too e.g. ab{3,}c will find abbbc, abbbbbbbbbbbbbbbbbbc, etc min isn't optional in Textpad, so use 0 instead e.g. ab{0,3}c will find ac, abc, abbc, abbc
  • 17.
    More character ranges Textpadalso has some "named" ranges you can use, e.g. [:alpha:] Any letter [:digit:] Any digit [:blank:] Space or tab [:space:] Space, tab, return, line feed, et al These go inside other [ ] e.g. [[:alpha:][:digit:]] finds any letter or number. See TextPad's Help (available from the Find dialogue) for more.
  • 18.
    Character ranges Writing [0123456789]every time is silly. Instead, use [...-...] (dash) to specify a range of characters. e.g. [0-9] to find any single number [a-z] to find any letter These can be combined with themselves, plus other characters e.g. [a-z0-9:.] to find any letter, number, colon or period
  • 19.
    Line and wordendings n Lets you look for line breaks, e.g. Hellonworld will find Hello world < and > Match the start and end of a word ^ Match the start of a line e.g. ^: will find any colon at the start of a line $ Match the end of a line e.g. .$ will find any period at the end of a line Combine these to match whole lines, e.g. ^?.*$ to match all lines that start with a question mark
  • 20.
    Getting more complicated Use(...) to set up "groups" - these can then be checked and counted in themselves e.g. (hello)+ will find hello, hellohello, hellohellohello, etc. (hello|bye){2} will find hellohello, byebye, hellobye and byehello Hello (Tom|Stefan|Emma) will look for any of the 3 names (t[io]ck){2}! will find ticktock!, tocktick!, ticktick!, and tocktock!
  • 21.
    More examples tofigure out What do the following regular expressions do? ^[0-9]{3} ^(Source|[0-9]{3}) ^.*[0-9]{3}$
  • 22.
    Replacements F8 As expected, Textpad will replace anything matched, with the text specified.
  • 23.
    Replacements Remember though: The REwill (only/completely) match what you've typed. Textpad will replace what's been matched. Work out what's being matched and what's being replaced. e.g. We may want to  match (find) all lines ending with a ?, but only replace the word before the ?
  • 24.
    Replacements A more usefulexample: [a-z]+[?!]$ Find any lines ending in letters, then ? or ! We want to replace the letters (i.e. [a-z]+) with something, but keep the ? or ! in place But we don't know which one to use when replacing...
  • 25.
    Replacements Luckily, we canrefer to things that have been matched using (...) groups. Each group has its own number (in sequential order), and can be inserted using n e.g. [a-z]+([?!])$ has one (...) group: 1 To replace the letters, we can use new word1 So hello! would become new word!
  • 26.
    Replacements Textpad uses 1to 9 - which we can use wherever we want e.g. ([a-z]+) ([0-9]+) will match Pelham 123 Replacing this with 2 1 gives us 123 Pelham Replacing it with 22 gives us 123123 (Be a bit careful if a (...) group is optional though, i.e. (...)? - if it's not found, your higher numbers will be wrong.)
  • 27.
    Phew... Can get trickyquickly, but remember: o Use the "Help" button o Use the "Find Next" button to check what your RE finds o Check the Cheat Sheet - http://bit.ly/textpadref
  • 28.
    Handy Tip 1:Multiple files To replace across many files, open all the files and tick "All documents" when replacing:
  • 29.
    Handy Tip 2:Searching a folder To look through (closed) files in a directory, use Ctrl + F5. Tick "Search subfolders" if folders are several layers deep.
  • 30.
    Handy Tip 3:Save everything Finally: You can see which files have changed - these have a * next to their filename. You can also save all changes to all open files using: File > Save All
  • 31.
    Other Textpad UsefulBits Block select mode (Ctrl+Q, then B): Bookmarked lines: Cut/copy/delete everything selected or bookmarked. Bookmarks
  • 32.

Editor's Notes

  • #4 I like Textpad as a general-purpose editor AND a coding tool, because it all comes down to a *lot* of techie tools to control data. And text is just another form of data, really. When you&apos;re editing it.
  • #5 Here&apos;s what you see when you fire up Textpad, probably.
  • #6 You can have many files open at once. They&apos;ll appear in the Document Selector on the left, and in tabs along the top of the editing window.
  • #7 Basic set-up follows...
  • #8 I like to allow multiple instances so I can have a different set of files open in each. I like to replace Notepad because I hate Notepad. Putting a context menu shortcut in means you can right-click a file in Windows Explorer, and choose to open it in Textpad. Handy. (Also, if youselected the first 2 options, then choosing &amp;quot;Edit&amp;quot; when right-clicking a file will open the file in a new Textpad instance.)
  • #9 These are the 3 main dialogue boxes we&apos;ll be using here. They have menu options under the &amp;quot;Search&amp;quot; menu as well.
  • #10 Regular Expressions. Scary, but they really should be taught to children as young as 3 or 4, IMHO.
  • #11 I&apos;m fairly sure that the human brain runs on regular expressions. Probably in Perl.
  • #12 You&apos;ll need some text open in Textpad to try this out on, of course.
  • #14 Basically just brackets then. And dot, question mark, star, plus, pipe, caret and dollar. And backslash. If in doubt, you can always switch off regular expressions for a search. * Other regexp environments, e.g. unix, perl, PHP, etc allow other matches, but the principles are basically the same. See later slides for some other info.
  • #16 I think of these like this: ? = &amp;quot;maybe&amp;quot; * = &amp;quot;any&amp;quot; + = &amp;quot;some&amp;quot;
  • #19 Textpad is only case-sensitive if you set the &amp;quot;Match case&amp;quot; option when searching. If that was on, you&apos;d need to use [a-zA-Z] to catch *all* letters, whatever case they were. Notice that the period on the last line is escaped, otherwise it would match *everything*.
  • #20 * &amp;quot;Hello\\nworld&amp;quot; example assumes that &amp;quot;world&amp;quot; is at the start of the next line.
  • #21 For want of a better word than &amp;quot;groups&amp;quot;
  • #22 Answers: 1. Find any 3 digits at the start of a line 2. Find any 3 digits OR the word &amp;quot;Source&amp;quot; at the start of a line 3. Fine any *line* (the whole line) that ends in 3 digits (could be more than 3 - these would still get matched)
  • #24 Probably the most confusing slide here. The next slide clarifies it a bit.
  • #27 Also, nested brackets can get tricky - numbers are assigned in the order of *starting* brackets, I believe.
  • #28 Cheat Sheet to be available soon
  • #30 This will list the results in a search results pane in Textpad. You can double-click file names in this to open them. I&apos;m not sure the best way to open *lots* of files though.
  • #33 Where &amp;quot;Perl&amp;quot; can be read as &amp;quot;Textpad&amp;quot;. For now. Oh, and here&apos;s a Perl RegExp to look for e-mail addresses (untested, there may be better ones): ^(\\w|\\-|\\_|\\.)+\\@((\\w|\\-|\\_)+\\.)+[a-zA-Z]{2,}$ (Won&apos;t work in Textpad though. Left as an exercise for the reader.) See also  http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html