Textpad and Regular Expressions


Published on

A primer for more advanced search and replacing in Textpad.

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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're editing it.
  • Here's what you see when you fire up Textpad, probably.
  • You can have many files open at once. They'll appear in the Document Selector on the left, and in tabs along the top of the editing window.
  • Basic set-up follows...
  • 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 "Edit" when right-clicking a file will open the file in a new Textpad instance.)
  • These are the 3 main dialogue boxes we'll be using here. They have menu options under the "Search" menu as well.
  • Regular Expressions. Scary, but they really should be taught to children as young as 3 or 4, IMHO.
  • I'm fairly sure that the human brain runs on regular expressions. Probably in Perl.
  • You'll need some text open in Textpad to try this out on, of course.
  • 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.
  • I think of these like this: ? = "maybe" * = "any" + = "some"
  • Textpad is only case-sensitive if you set the "Match case" option when searching. If that was on, you'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*.
  • * "Hello\\nworld" example assumes that "world" is at the start of the next line.
  • For want of a better word than "groups"
  • Answers: 1. Find any 3 digits at the start of a line 2. Find any 3 digits OR the word "Source" 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)
  • Probably the most confusing slide here. The next slide clarifies it a bit.
  • Also, nested brackets can get tricky - numbers are assigned in the order of *starting* brackets, I believe.
  • Cheat Sheet to be available soon
  • This will list the results in a search results pane in Textpad. You can double-click file names in this to open them. I'm not sure the best way to open *lots* of files though.
  • Where "Perl" can be read as "Textpad". For now. Oh, and here's a Perl RegExp to look for e-mail addresses (untested, there may be better ones): ^(\\w|\\-|\\_|\\.)+\\@((\\w|\\-|\\_)+\\.)+[a-zA-Z]{2,}$ (Won't work in Textpad though. Left as an exercise for the reader.) See also  http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
  • Textpad and Regular Expressions

    1. 1. Textpad and Finding Things A primer for the uninitiated Graham Lally April 2012 graham.lally@ocsi.co.uk http://www.ocsi.co.uk/
    2. 2. Coming up• Hello Textpad• Learning to love Regular Expressions• Other useful things
    3. 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/
    4. 4. Here are thefiles youhave open Write things here
    5. 5. Configure > Preferences
    6. 6. Basic set-up C Allow multiple instances to run C Replace Notepad C Put shortcut to Textpad on: Context MenuAlso set under "Editor" settings: C Use POSIX regular expression syntaxThis means we can use ( ) and { } instead of ( ) and { }
    7. 7. Shortcuts to get started Search current file(s) F5 Search and replace F8 Search through a whole directory Ctrl + F5
    8. 8. Regular ExpressionsWhy (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 Kings X Kings X Kings Cross Kings Cross Kings Cross Kings Cross
    9. 9. 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.
    10. 10. My first regular expression F5: C Regular expression Find: .a
    11. 11. ".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, %aAnd "a.c" will find abc, aac, a6c, a?c, a c, a[c, a-c, etc
    12. 12. Characters to watch out forIn Textpad*, most characters do what they should.Some dont: . ? * + | [ ] ( ) { } ^ $To actually look for one of these, put a "" before it. e.g. ? (To look for a , use (* Other RegExp software may differ)
    13. 13. 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
    14. 14. Counting thingsUse +, * or ? after a character to indicate how many times itshould 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
    15. 15. Counting thingsUse {min,max} after a character to set your own limits e.g. ab{4,6}c will find abbbbc, abbbbbc and abbbbbbcmax is optional too e.g. ab{3,}c will find abbbc, abbbbbbbbbbbbbbbbbbc, etcmin isnt optional in Textpad, so use 0 instead e.g. ab{0,3}c will find ac, abc, abbc, abbc
    16. 16. More character rangesTextpad 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 alThese go inside other [ ] e.g. [[:alpha:][:digit:]] finds any letter ornumber.See TextPads Help (available from the Find dialogue) formore.
    17. 17. Character rangesWriting [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 letterThese can be combined with themselves, plus other characters e.g. [a-z0-9:.] to find any letter, number, colon or period
    18. 18. 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 lineCombine these to match whole lines, e.g. ^?.*$ to match alllines that start with a question mark
    19. 19. Getting more complicatedUse (...) to set up "groups" - these can then be checked andcounted in themselvese.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!
    20. 20. More examples to figure outWhat do the following regular expressions do? ^[0-9]{3} ^(Source|[0-9]{3}) ^.*[0-9]{3}$
    21. 21. Replacements F8As expected, Textpad will replace anything matched, with thetext specified.
    22. 22. ReplacementsRemember though:The RE will (only/completely) match what youve typed.Textpad will replace whats been matched.Work out whats being matched and whats being replaced.e.g. We may want to  match (find) all lines ending with a ?, butonly replace the word before the ?
    23. 23. ReplacementsA 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, butkeep the ? or ! in placeBut we dont know which one to use when replacing...
    24. 24. ReplacementsLuckily, we can refer to things that have been matched using(...) groups.Each group has its own number (in sequential order), and canbe inserted using ne.g. [a-z]+([?!])$ has one (...) group: 1 To replace the letters, we can use new word1 So hello! would become new word!
    25. 25. ReplacementsTextpad uses 1 to 9 - which we can use wherever we wante.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. (...)? - ifits not found, your higher numbers will be wrong.)
    26. 26. 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
    27. 27. Handy Tip 1: Multiple filesTo replace across many files, open all the files and tick "Alldocuments" when replacing:
    28. 28. Handy Tip 2: Searching a folderTo look through (closed) files in a directory, use Ctrl + F5. Tick"Search subfolders" if folders are several layers deep.
    29. 29. Handy Tip 3: Save everythingFinally:You can see which files have changed - these have a * next totheir filename.You can also save all changes to all open files using: File > Save All
    30. 30. Other Textpad Useful BitsBlock select mode (Ctrl+Q, then B): Bookmarked lines:Cut/copy/deleteeverything selectedor bookmarked. Bookmarks
    31. 31. http://xkcd.com/208/