Page 1 of 1

How do I find and replace only the first occurance

Posted: Thu Jun 18, 2009 7:22 pm
by richwill777
I have addresses in the following form:
1724 26th Ave NW~Gig Harbor~WA~98335~United States

I need to replace the ~, one at a time.

Therefore, I need an "anchor" type that will stop at the first occurance.

Thanks...

Richard

Posted: Thu Jun 18, 2009 10:37 pm
by Bob Hansen
Use FIND vs. Replace

Find what: ^[^~]*~

Click on Find Next.
This will select all of the beginning text and the first "~".

Close the Find window, press the right arrow, and you will have the cursor just past the first tildel

You can then edit the tilde, and move along the line to each following "~" to change as desired. At the end of the line, do Find Next(CTL-F), to go to the next line.

Use the following settings:
-----------------------------------------
[X] Regular expression
-----------------------------------------
Configure | Preferences | Editor
[X] Use POSIX regular expression syntax
-----------------------------------------

How do I find and replace only the first occurance

Posted: Fri Jun 19, 2009 12:46 am
by richwill777
Bob:

Thanks........ That came close.
My test was to replace the ~ with a "," comma.
However, it replaces the string up thru the ~ with a "," comma.

Therefore:
1724 26th Ave NW~Gig Harbor~WA~98335~United States

Becomes:

,Gig Harbor~WA~98335~United States

Almost there !!

I played with it a little and got several other "almost" results.
Something in your suggestion grabs all the text up thru the ~,
and not just the ~.

RegEx and POSIX are set.

The "stop at first occurance" seems to work.......

Any thoughts ??

Richard Williams

Posted: Fri Jun 19, 2009 3:35 am
by Bob Hansen
It sounded like you wanted to manually replace the "~". My instructions were to use FIND, not replace. So I was trying to get you to the position of the first tilde for manual changes.

I you want to replace the first "~" with a "," then try this:

Use REPLACE vs. FIND

Find what: ^([^~]*)~
Replace with: \1,

To do the same replacement in all lines, click on Replace All

To manually select the changes, click on Replace Next, and repeat to go to the next line for the same RegEx replacement values, or replace the comma in the "Replace with" field to use something different from the ","

Note: Once you have replaced the first "~" in each of the lines, if you go back to the top and run the same replace again, it will now pick up the next "~" on the lines. The former second tilde is now the first since the original first one was replaced. Repeat again for each successive tilde.

How do I find and replace only the first occurance

Posted: Fri Jun 19, 2009 4:51 am
by richwill777
Bob:

Thanks for the response….

I am using Ver 4.7.3 32 bit
Regular Expression is Checked.
I am using Replace not Find;
And I test it on one line (record), then on 20 records;
and I have 39,000 records to fix (Replace All).
The plan is to move across the record one ~ at a time:
\n then , then space then \n will make it look like an address.

Its treating the Replace String as a literal, not as Reg Ex.:

Find: ^[^~]*~
Replace with: \1,

724 26th Ave NW~Gig Harbor~WA~98335~United States
So, I get:
\1,Gig Harbor~WA~98335~United States

Do I have to set another switch someplace to treat the Replace String as Reg Ex ??

Richard

How do I find and replace only the first occurance

Posted: Fri Jun 19, 2009 4:58 am
by richwill777
Bob:

False Alarm.......

I didn't see your change to the Find Reg Ex
^[^~]*~ to ^([^~]*)~

That works perfectly !!

I'm of the the Book store to get the
Reg Ex Cookbook by Jan Goyvates

Thanks again.....

Richard

Posted: Fri Jun 19, 2009 9:25 am
by ben_josephs
I'm not familiar with that book.

While you're in the bookshop have a look at

Friedl, Jeffrey E F
Mastering Regular Expressions, 3rd ed
O'Reilly, 2006
ISBN: 0-596-52812-4
http://regex.info/

which is the standard reference for regular expressions.

Posted: Fri Jun 19, 2009 1:44 pm
by richwill777
Bob:

I will.....

See, 6 lines down; an Info Post by SteveH:

I've just seen that a new book on Regular Expressions has been released. Regular Expressions Cookbook is written by Jan Goyvaerts (author of RegexBuddy and regular-expressions.info) and Steven Levithan (author of XRegExp and RegexPal) and released by O'Reilly.

I haven't had a chance to buy a copy yet but think it looks like a good book on the subject and a good addition to the classic Mastering Regular Expressions by Jeffrey Friedl

Thanks again....

Richard

Posted: Fri Jun 19, 2009 1:51 pm
by Bob Hansen
I also have never used the Goyvaerts book recently referenced, but like ben_josephs, I have been using Jeff Friedl's book for a long time now.

Posted: Fri Jun 19, 2009 2:00 pm
by Art Metzer
Coding Horror's Jeff Atwood recently commented on Goyvaerts' Regular Expressions Cookbook vis-a-vis Friedl's book:
In my mind, at least, [Goyvaerts' Regular Expressions Cookbook] completely replaces the Friedl book as the go-to reference for programmers of any skill level or background who seek regular expression enlightenment.
YMMV.

Art

Posted: Fri Jun 19, 2009 6:21 pm
by Bob Hansen
Thanks for the review, Art. Will have to check out the new book.

And here is an online testing tool, roll over the expressions to see what they do, see results as you type, many nice features....
http://www.gskinner.com/RegExr/