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 even bother trying to understand the next bit - you won't. 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.
You may still think that looks a bit feeble. But just imagine an excel spreadsheet where a similar change had to be made. Now imagine it was 2000 lines long, and hopefully, you'll start to see the potential...

No comments:
Post a Comment