Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Sunday, 21 December 2008

10: Find and Replace

Here is Textpad's 'find and replace' box. 

We open it by using F8.



You'll notice that most of the options are the same as ''find' - see post 7 for an explanation of these. There are some differences: 

No wrapped search. 'Find next' only works downwards to the end of the file.
Selected text. You can highlight some text, and make changes in the highlighted text only. 

What we're interested in here are the Go buttons. Once you've typed what you want to find into the 'Find' box, choose between:

Find next Finds the next occurence of what's in the 'find what' box
Replace Replaces the current selection with what's in the 'replace with' box
Replace next Replaces the next match of the 'find what' box' with what's in the 'replace with' box
Replace all Replaces every single match of the 'find what' box' with what's in the 'replace with' box

You will almost always use Replace all.


Monday, 15 December 2008

9: Using 'Find' - the magic of 'Mark all'

If you scroll back a way, you'll find my unbelievably bad spreadsheet of 'books on my coffee table'. Well, here it is again, this time to show the power of 'Mark all'.

We open the find window with F5. 



So: I've looked for books with the publisher 'Penguin'. (Note that I've used \tPenguin\t, which means 'tab Penguin tab'. Otherwise, I might accidentally mark a book with the title 'I love my penguin', which isn't what I want.) 

And there they are - two books which match \tPenguin\t, marked with a little triangle called a 'bookmark'.

Now I want to do something with these bookmarked lines. Here's what I can do:

Cut the bookmarked lines 
Copy the bookmarked lines 
Delete the bookmarked lines

I want to copy them to a new document to see them more closely. I go here:



I open a new document, and I paste my bookmarked lines:



It turns out that both my Penguin books are by PG Wodehouse. Now I can look over the data, modify it, whatever I want. 

The only thing I can't do is modify the data and put it back in it's original place. Bookmarking only works in one direction. If I wanted to modify the records and then put them back in the original file, I would have to cut them from this document and paste them onto the end of the original.

Sunday, 9 November 2008

4. Regular expressions: introduction

'Regular expressions' is a difficult name for a useful tool. Basically, they're very small computer programs. A single regular expression (or 'regex') is a little sequence of special characters that match a pattern in a file.

What we're dealing with here is 'find' and 'replace'.

Consider this little piece of text:

I have a cat because I like cats. I can never catch my cat.

If you wanted to change that to 'dog', in Excel, or Word, you could. You find 'cat' and replace it with 'dog'.

Find: cat
Replace: dog
Output: I have a dog because I like cats. I can never catch my dog.

Excel and Word will skip the word 'cats'. These programs know that you're unlikely to want to change every sequence of c-a-t, so they assume you mean "find 'cat', but only when it's definitely one word and not part of another word, like 'catch' or 'replicate' or 'subcategory'". Fair enough. Easy, too - after all, you can just do a second find-replace for 'cats'.

Textpad isn't that generous. It assumes that you mean exactly what you say, every time, with no exceptions.

In textpad, this would happen:

Find: cat
Replace: dog
Output: I have a dog because I like dogs. I can never dogch my dog.

The obvious response to that example is: That's really stupid.

Absolutely it's stupid. But here's the thing: Finding 'cat' and replacing it with 'dog' is not a regex. (As a general rule, the less text there is in a regex, the better). Regexes are not about text, they are about patterns, and the patterns are expressed in metacharacters.

A metacharacter is a single keyboard-stroke that refers to a type of character, not the character itself.

Here are some examples:

. means 'any character'
? means 'zero or one of the character before the ?

Don't bother trying to understand the next bit. The point of this example is not to show how it's done, only that it can be done. (The red is just to show which bits are doing the work).

Find: cat\(s?\)\>
Replace: dog\1
Output: I have a dog because I like dogs. I can never catch my dog.

And there we go. All the changes have been made accurately and cleanly in one move.