Textpad and Regular Expressions

April 6, 2018 | Author: Anonymous | Category: Technology
Report this link


Description

1. Textpad and Finding Things A primer for the uninitiatedGraham Lally April 2012 [email protected]://www.ocsi.co.uk/ 2. Coming up• Hello Textpad• Learning to love Regular Expressions• Other useful things 3. Why Textpad? +Text-editing Lots of optionshttp://www.textpad.com/ http://www.flickr.com/photos/eldholm/2354982554/http://www.flickr.com/photos/grandmaitre/2387376806/ 4. Here are thefiles youhave open Write things here 5. Configure > Preferences 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. Shortcuts to get started Search current file(s) F5 Search and replace F8 Search through a whole directory Ctrl + F5 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 XKings XKings XKings Cross Kings CrossKings CrossKings Cross 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. My first regular expression F5: C Regular expressionFind: .a 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 ... .aFind any character followedby an "a", e.g. ba, 1a, %aAnd "a.c" will find abc, aac, a6c, a?c, a c, a[c, a-c, etc 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. 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. 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. Counting thingsUse {min,max} after a character to set your own limitse.g. ab{4,6}c will find abbbbc, abbbbbc and abbbbbbcmax is optional tooe.g. ab{3,}c will find abbbc, abbbbbbbbbbbbbbbbbbc, etcmin isnt optional in Textpad, so use 0 insteade.g. ab{0,3}c will find ac, abc, abbc, abbc 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. 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 characterse.g. [a-z0-9:.] to find any letter, number, colon or period 18. Line and word endingsn 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 linee.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. 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 byehelloHello (Tom|Stefan|Emma) will look for any of the 3 names(t[io]ck){2}! will find ticktock!, tocktick!, ticktick!, and tocktock! 20. More examples to figure outWhat do the following regular expressions do?^[0-9]{3}^(Source|[0-9]{3})^.*[0-9]{3}$ 21. Replacements F8As expected, Textpad will replace anything matched, with thetext specified. 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. 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. 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. 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. 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. Handy Tip 1: Multiple filesTo replace across many files, open all the files and tick "Alldocuments" when replacing: 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. 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. Other Textpad Useful BitsBlock select mode (Ctrl+Q, then B): Bookmarked lines:Cut/copy/deleteeverything selectedor bookmarked.Bookmarks 31. http://xkcd.com/208/


Comments

Copyright © 2024 UPDOCS Inc.