Page 1 of 1

Understanding Replace with:

Posted: Fri Oct 23, 2009 4:34 pm
by BBowers
I'm sorry to post this, but I can't find what I'm looking for by searching the forum. I'm having a hard time understanding the \1\2 etc. in the Replace with:

Example: note, there is a space before and after each state abbrev.

52 E Columbia St: Othello: WA (509) 488-2312
201 State Highway 59: Douglas: WY (307) 358-4428
101 Missouri Ave: Deer Lodge: MT (406) 846-2525

What I want:

52 E Columbia St: Othello,WA,(509) 488-2312
201 State Highway 59: Douglas,WY,(307) 358-4428
101 Missouri Ave: Deer Lodge,MT,(406) 846-2525

I find all the two letter State abbrev. that has the : (colon) in front of it, although I'm sure this is not the best way to find it, by Find what: :_ .._ 'underscore meaning a space'

In the Replace with: ,\1,
Obviously this is not correct because it just replaces the State abbrev. with ,\1,

I tried Replace with: ,\0,
Which does the replace but it doesn't replace the : (colon) or the spaces.

I've found quite a bit of information on how to search in TextPad, but I haven't found a lot on all the different Replacement patterns.
Is there a good reference that explains things in layman terms when it comes to Replace with:?

Posted: Fri Oct 23, 2009 11:23 pm
by Bob Hansen
Search for: :_([A-Z]{2})_\(
Replace with: ,\1,(

The _ represents a space character
==========================
Use the following settings:
-----------------------------------------
[X] Regular expression
Replace All
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------
=========================

Syntax explanation:
Search for:
:_ ............(colon followed by a space)
([A-Z]{2}) ............(tagged group of two consecutive UpperCase characters)
_\( ............(space followed by an opening parenthesis)


Replace with:
, ............(a comma)
\1 ............(the contents of the first tagged group = ST)
,( ............(a comma followed by an opening parenthesis)

==========================
Resources:
The TextPad HELP sections on "Replacement Expressions" and "How to Use Regular Expressions" show the (.....) for grouping expressions for use in replacements.

Two good text books on Regular Expressions:
Mastering Regular Expressions, Jefrey Friedl, O'Reilly Publications, $40. I have used this for a number of years.
Regular Expressions Cookbook, Jan Goyvaerts * Steven Levithan, O'Reilly Publications, $45. Just released this past year, is a good book to complement Friedl's book.

Also look through this forum for examples of other solutions. Do an "autopsy" on them to help understand the syntax.

Posted: Sat Oct 24, 2009 2:33 am
by BBowers
Thanks Bob

Your explanation is very helpful. I learn a little more each day.