Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / String Replace Function Help

Author
Message
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Feb 2013 12:37 Edited at: 18th Feb 2013 13:52
I've been working on this function for a few weeks now and I just can't get it right, it's worked for some of the strings I wanted to replace (converting a DBC project) but I'm being quite demanding of it and it's getting complicated.

The function must have four key features:
* It must search for multiple instances of the find string within the source string.
* It reads "a~b" as accepting anything in between a and b, like most search function use "*" but "~" is an easier symbol to use since it is not in DB code.
* It must also read "~" in the replace string so that you can replace only the search terms either side of "~" but preserve the text in between, eg: source = "mary had a little lamb.", find = "mary~lamb", replace = "dexter~lab" becomes "dexter had a little lab."
* Finally, if matching brackets are in the search terms they must be preserved, i.e. source = "((()))" find = "(~)" should take "((()))" NOT "((()"

Here's my semi-broken code:


Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Feb 2013 17:46
So 'mary' and 'lamb' must both appear in a search and lamb has to be found at some point after the word mary? Do I understand that correctly?

Quote: "Finally, if matching brackets are in the search terms they must be preserved, i.e. source = "((()))" find = "(~)" should take "((()))" NOT "((()""

This is the one that I feel doesn't make sense. If you're searching for (~) then ((() would fit that. I don't see a logical reason why you would force matching brackets if this is just a string replace function.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 18th Feb 2013 17:56
Are you able to use a RegEx (Regular Expression) DLL? They make pattern matching a breeze, and it will do all of the things you need.

One example: http://gnuwin32.sourceforge.net/packages/regex.htm

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Feb 2013 19:24 Edited at: 18th Feb 2013 19:26
Quote: "So 'mary' and 'lamb' must both appear in a search and lamb has to be found at some point after the word mary? Do I understand that correctly?"

Yes that is it exactly.

Quote: "This is the one that I feel doesn't make sense. If you're searching for (~) then ((() would fit that. I don't see a logical reason why you would force matching brackets if this is just a string replace function."

I am primarily using this for converting arrays to a new format so I want to avoid stuff like this happening:
source = "ship(x,2) = planet(p,1)"
search = "ship(~,1)"
replace = "ship(~).class"
result = "ship(x,2) = planet(p).class"

Quote: "Are you able to use a RegEx (Regular Expression) DLL? They make pattern matching a breeze, and it will do all of the things you need."

I've never used a dll before but this might be a good one to start with, thanks.

Login to post a reply

Server time is: 2026-07-07 10:59:16
Your offset time is: 2026-07-07 10:59:16