Understanding Replace with:

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
BBowers
Posts: 17
Joined: Fri Jan 05, 2007 4:19 pm
Location: Colorado

Understanding Replace with:

Post 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:?
User avatar
Bob Hansen
Posts: 1516
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post 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.
Last edited by Bob Hansen on Sat Oct 24, 2009 5:23 pm, edited 2 times in total.
Hope this was helpful.............good luck,
Bob
BBowers
Posts: 17
Joined: Fri Jan 05, 2007 4:19 pm
Location: Colorado

Post by BBowers »

Thanks Bob

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