Replace routine - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Replace routine (/showthread.php?tid=3177) |
Replace routine - Pete - 10-31-2024 Have you guys noticed that "wrap" on searches like Notepad only work when a line-break in not encountered? That makes sense, I guess, but what if you wanted to ignore those line-breaks and find what you are looking for, anyway? Pete is tremendous but Steve is just amazing. So search that for Pete is tremendous. No go in most html editors or Notepad. I don't Word or OpenOffice, anymore, so I don't know if they would find the search or not. Anyway, I got a little burned out with trying a strict math model to accomplish this, so I combined a math and string replacement method to get the job done. It's super fast, but it has a drawback in that the file being searched cannot contain 3 substitute string characters I used to handle the line-break situation. Chr$(1), Chr$(3), and Chr$(4). Us SCREEN 0 card programmers are screwed for diamonds and hearts! So I'm curious if anyone has coded this and figured out a pure math method? Now the routine itself is actually very useful for html files, which often contain text broken up on different lines. I coded it to preserve line-breaks. I have not, and may not go to the extent of perfect line-break mirroring, as that would require checking the for the nearest space in the replacement term instead of stacking the line-breaks onto the end. Yes, it can handle search and replace over multiple line-breaks. For instance... Pete is tremendous but Steve is just amazing. Search for: Pete is tremendous Replace with: Pete is TREMENDOUS The results with my routine would be... Pete is TREMENDOUS but Steve is just amazing. instead of... Pete is TREMENDOUS but Steve is just amazing. ---------------------------------------- So not an exact mirror, but it does get the job, essentially, done. Here's the code. It won't change any of your files, or make any new ones. It just loads the edited content to your clipboard and opens Notepad. You can paste it in to view it. Just pick a text file (.bas, .txt, .html, etc.), or make a test file, to try it out. Code: (Select All)
Basically I'm just curious about my approach, different approaches, and use. If you can think of another use, different output, or different build approach, I'd enjoy reading the comments and engaging in the conversation. Pete RE: Replace routine - Kernelpanic - 10-31-2024 Interesting exercise, Pete! As an example, an HTML file: The search term is found and changed. The changed text can in this case also be copied into an HTML editor, of course. A problem arises, however, with a longer search term. That ist OK: That did not work: RE: Replace routine - bplus - 10-31-2024 It is an interesting challenge, the issue occurs even in our Basic IDE searches. @Pete glad to help get you to 100! Long overdue. RE: Replace routine - Pete - 10-31-2024 @bplus Thanks! Note to anyone using html to test this: The routine is not an html parser, meaning it will not search wrap tags like <br> or <p></P>. It would also have to handle crap like (non-breaking space) and other html coding for quote marks and other punctuation that could be present. I did not want to venture down that rabbit-hole for this utility. I did think up a way to determine the location in the original text of each search occurrence that needs to be replaced. This is just needed if the routine was to ever be incorporated into a wysiwyg or wp app. It would allow the text found to be highlighted. I also think I thought up a way to put arrays in place of the string characters that I used to mark the search and replace positions. I'll code it late and see if it pans out. Oh, @Kernelpanic In image #2 you searched for: Burg Eisenhardt in Belzig when you needed to search for 'Burg Eisenhardt' in Belzig Not including the ' marks in the search is why it didn't find it. The routine is not case-sensitive, but that is as AI as it gets. So except for case, the search term needs to match exactly. Thanks for testing it though. Very much appreciated! Pete |