Showing posts with label metadata regex regular_expressions metacharacter. Show all posts
Showing posts with label metadata regex regular_expressions metacharacter. 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.

Thursday, 11 December 2008

8: Find

If you're used to excel, or word, or any normal 'find' system, you're used to the idea that 'find' and 'find and replace' are pretty much the same thing. In textpad, they're not. 

F5 opens the 'find' window
F8 opens the 'find and replace' window

First up: Find. The find window looks like this:



Let's look at the options here:

'Text'. 'Hex' means 'Hexadecimal', and is advanced stuff. Always select 'Text'.

'Match whole words' means exactly that - match sets of characters that end with a space, a full-stop (or other punctuation), or a tab. This is very handy - it means that if you search for 'cat' it will find the exact word 'cat' and not 'concatenate'. Leave this box ticked.

'Match case' means exactly that - it will match capital and lower-case letters. This is handy if you want to find March (the month) but not march (walking in formation). It will mess up your results if you leave it on - a search for 'Animal' will not find 'animal' if this box is ticked. Usually, leave this box unticked.

 3. 'Regular expression'. When you check this box, you turn regexes on and off. If it's ticked, \t means 'tab'. If it's not, \t literally means \t. If your regex doesn't work, it is often because this box is not ticked. Always make sure this box matches what you want to do.

4. 'Wrap searches'. Textpad starts finding from the cursor mark and stops at the bottom of the document, which can be annoying. If you tick this box, it will go to the bottom, then back to the top and start again - which means the whole document gets searched. Leave this box ticked. 


Direction: Up and Down. If you really only want to search in one direction from the cursor point (i.e. not a wrap seach), you can choose the direction here. These are used infrequently.

Extend selection and In all documents. You can search is selected text, or in more than one open document, using these buttons. These are used infrequently.


...and now, the 'Go' buttons:

Find next means 'show me the next match after the cursor' (easy)

Mark all means 'put a marker next to all the matches in the file.' 

Mark all is super handy. Super, super, super handy - I cannot stress enought how handy it is. Read all about it in the next post.

Saturday, 29 November 2008

7: \n and \t

Here are two codes that you will use all the time:

\t means tab
\n means new line

Textpad is at its best when using tab seperated values (TSV), which is exactly what we find in excel spreadsheets. Below, you'll see data represented in a spreadsheet...

 

...and in textpad...



...and, finally, here is the first line of data as a 'find' line in textpad:

david nicholls\ttheunderstudy\tHodder & Stoughton\t1\n

So, wherever there is a tab, we represent it as \t

(Strictly speaking, there is enough detail here that the \n is uneccesary - it's just there to demonstrate. Another more accurate way to represent this is to use at the end of the line - it means 'end of the line'. More on this later.)


Thursday, 20 November 2008

6: Escaping the metacharacter

So: the obvious question. The question is 'but if I write a regular expression for some text, and there's a question mark in it, and it really means 'this is the end of the sentence and the sentence is a question', what do I do?'

Easy peasy. If the symbol has a special meaning that you don't want to use, you need to escape the special meaning. To do this, you use a metacharacter. 

Every time you mean to use a symbol as a character (i.e. not a metacharacter), you use this: \

So if you had this piece of text:

What is wrong with this damn computer?

And you wanted the ? to mean (the way it usually does) 'this is the end of the sentence and the sentence is a question', you would start your regex like this: 

find: What is wrong with this damn computer\?

The means 'literally'. It means 'this is not a metacharacter, it's just a nice normal character. I use at least once in every regex I ever write.

Warning: sometimes \ reverses, and indicates that the next character is, in fact, special (I know, it's confusing) The most frequently used examples of this are \n and \t\n means 'new line', and \t means 'tab'. More on these two in lesson seven.
 

Sunday, 16 November 2008

5. Two types of symbols (but they're the same symbols)

So, in the last entry, I introduced the idea of metacharacters - symbols that mean something. There's a list of them on the right there ('know your metacharacters').

Text is always comprised of symbols. Letters are symbols. Punctuation marks are symbols. When using regexes, we break down symbols into two types: characters and metacharacters, which can be thought of as 'normal' and 'special'.

Here's the tricky bit: When we use regexes, we are invariably using characters and metacharacters in the same piece of text.

So there are a bunch of symbols that can mean either what they normally mean in English (or any other language) or a special pattern matching instruction. (btw - yes, there is an easy way to tell the difference. I'll get to that.)

Here's an example: The symbols . and ?

AS CHARACTERS

. means 'this is the end of the sentence'
? means 'this is the end of the sentence, and this sentence is a question'

AS METACHARACTERS

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

Totally different. The best thing to do here is ignore your life-long understanding of what . and ? mean as characters - the relationship between the character meaning and the metacharacter meaning is completely arbitrary.

This still leaves us with the question: If they're the same, how do you tell them apart?