How do I find and replace only the first occurance
Moderators: AmigoJack, bbadmin, helios, MudGuard
-
richwill777
- Posts: 5
- Joined: Thu Jun 18, 2009 7:03 pm
How do I find and replace only the first occurance
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
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
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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
-----------------------------------------
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
-----------------------------------------
Hope this was helpful.............good luck,
Bob
Bob
-
richwill777
- Posts: 5
- Joined: Thu Jun 18, 2009 7:03 pm
How do I find and replace only the first occurance
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
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
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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.
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.
Hope this was helpful.............good luck,
Bob
Bob
-
richwill777
- Posts: 5
- Joined: Thu Jun 18, 2009 7:03 pm
How do I find and replace only the first occurance
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
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
-
richwill777
- Posts: 5
- Joined: Thu Jun 18, 2009 7:03 pm
How do I find and replace only the first occurance
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
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
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
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.
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.
-
richwill777
- Posts: 5
- Joined: Thu Jun 18, 2009 7:03 pm
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
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
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
-
Art Metzer
- Posts: 27
- Joined: Mon Mar 06, 2006 5:31 pm
Coding Horror's Jeff Atwood recently commented on Goyvaerts' Regular Expressions Cookbook vis-a-vis Friedl's book:
Art
YMMV.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.
Art
- Bob Hansen
- Posts: 1516
- Joined: Sun Mar 02, 2003 8:15 pm
- Location: Salem, NH
- Contact:
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/
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/
Hope this was helpful.............good luck,
Bob
Bob