|
Forum Index : Microcontroller and PC projects : Does MMBasic have a REPLACE function (or sub)
| Author | Message | ||||
| thwill Guru Joined: 16/09/2019 Location: United KingdomPosts: 4424 |
Hi folks, Does MMBasic have a single function/sub that does string matching and replacement yet? - I'm not aware of anything up to v6 but I haven't been paying full attentions since then. Thanks, Tom Edited 2026-11-25 08:37 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
| homa Guru Joined: 05/11/2021 Location: GermanyPosts: 790 |
Not exactly string, BUT LONGSTRING REPLACE array%() , string$, start Will substitute characters in the normal MMBasic string ‘string$’ into an existing long string ‘array%()’ starting at position ‘start’ in the long string. OR Appendix E – Regex Syntax Regex Syntax The alternate forms of the INSTR() and LINSTR() functions can take a regular expression as the search pattern. The alternate form of the functions are: INSTR([start],text$, search$ [,size]) LINSTR(text%(),search$ [,start] [,size] Edited 2026-11-25 09:11 by homa |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6647 |
Have a play with INSTR and MID$ or you might prefer regex Jim VK7JH MMedit |
||||
| Turbo46 Guru Joined: 24/12/2017 Location: AustraliaPosts: 1695 |
I found this on Fruit of the Shed. Function Replace$(a$,b$,c$) Local Integer z,d,e Local r$,x$ z=1:d=Len(c$):e=Len(b$):r$=a$ Do z=Instr(z,r$,b$) If z=0 Then Exit Do x$=Left$(r$,z-1) r$=x$+c$+Mid$(r$,z+e) z=Len(x$)+d Loop Replace$=r$ End Function and asked Claude if it could improve it. The response: There's a bug in the original, plus a couple of smaller issues. The bug: after a replacement, z=Len(x$)+d points at the last character of the replacement, not the one after it. If the replacement ends with something that matches the start of the search string, it gets rescanned. Replace$("a","a","aa") loops forever, because each pass finds the "a" it just inserted. When c$ is empty, z can also become 0, which is a bad start position for Instr. Other issues: An empty b$ isn't guarded against. It rebuilds the whole string on every match. Walking forward and appending is simpler and avoids the position arithmetic that caused the bug. Function Replace$(a$,b$,c$) Local Integer z,p,e Local r$ e=Len(b$) If e=0 Then Replace$=a$ : Exit Function p=1 Do z=Instr(p,a$,b$) If z=0 Then Exit Do If z>p Then r$=r$+Mid$(a$,p,z-p) r$=r$+c$ p=z+e Loop Replace$=r$+Mid$(a$,p) End Function Because it only ever searches the original string a$, replacement text is never rescanned, so the infinite-loop case can't occur. The behaviour is unchanged otherwise. One limitation remains: MMBasic strings max out at 255 characters, so if the replacement lengthens the string past that, you'll get a "String too long" error. If that's likely in your use, I can add a length check that truncates or returns the original. My thought is that it is better to have it error out than silently either do nothing or truncate the result. Bill Keep safe. Live long and prosper. |
||||
| twofingers Guru Joined: 02/06/2014 Location: GermanyPosts: 1786 |
Hi Tom, if it’s just a matter of performing replacements within text files, the latest version of the original FM (Javavis) might be of help. But I assume you’re simply looking for the relevant function ...? Regards Michael |
||||
| thwill Guru Joined: 16/09/2019 Location: United KingdomPosts: 4424 |
Thanks folks, I was already using an INSTR + MID$ version, I was just wondering if amongst all the new functionality there was a single-call implementation. My adventure game (still in progress) performs a lot of string replacement to canonicalise the player's input before matching against the suspect's dialogue patterns. No worries, I've got over it. Thanks, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |