Page 1 of 2

replace words in a string

Posted: Sat Jan 19, 2013 9:23 pm
by Jay
hi giys

here my attempt at a replace all words in a string module done in ruby! it takes the count of 50 odd components used in the original green version on the SM forum down to 1 or (6 including the connectors)

it replaces all instances of a word within a string, case sensitive!

you will see that i have done it a strange way using gsub with " " added before and after the find word and the replace word! this is because i could not fathom out how to do reg expressions around vars and i needed a way to get it to add a white space around the word to ensure whole words were changed and not partials

if anyone fancies fixing out the reg ex that would be cool :)

replace words in a string (case sensitive).fsm
(1.24 KiB) Downloaded 1474 times


best regards

Re: replace words in a string

Posted: Sun Jan 20, 2013 4:18 pm
by trogluddite
The regExp you seek is...

Code: Select all

/\b#{@find}\b/


Where...
/ = the 'brackets' to delimit the regExp
#{@find} means "substitute this Ruby expression, @find, to be the string to look for"
\b is a marker that indicates a "word boundary" - i.e. anywhere that white-space and text characters are side-by-side. By putting \b before and after the search string, it will only find whole words, for any white-space character, including the start/end of lines, tabs, etc.
Anywhere that you see a backslash in a RegExp, it means that the next character is a tag of some sort - took me a while to figure that one out, the API doc's for RegExp's are not at all clear!

Re: replace words in a string

Posted: Sun Jan 20, 2013 11:46 pm
by Jay
fantastic Trogg!

thanks for taking the time to help m8!

I wonder if the Ruby interpreter laughs at my code! ;)

so where i was going wrong was i was putting the regexp after the gsub and around (find, rep) and not @find input and all in the wrong place ha ha!

so you perform the regexp on the input rather than the variable its read to! I under stand the regexp a bit better now also due to your reply! i need to go study up on this now!

you know its just wonderful that even doing things the wrong way still works in ruby!

I will add it to the collection, they will be handy for ppl who have neither the want nor intention of getting their hands wet with code

Best Regards

Re: replace words in a string

Posted: Mon Jan 21, 2013 12:15 pm
by trogluddite
Jay wrote:you know its just wonderful that even doing things the wrong way still works in ruby!

Yeah, it kind of spoils you for choice, there's often so many ways to do the same thing that it's really hard to choose! :)

Jay wrote:so you perform the regexp on the input rather than the variable its read to

Yes, a RegExp defines a search to be done on an input string. Each time a match is found, some data gets returned about where in the string it found the match etc. - which can then be used by whatever code needs to know.
But that ain't so easy to see when it is all wrapped inside the 'gsub' method - the RegExp is only a small part of what it is doing, and there's no way to peek inside!

Re: replace words in a string

Posted: Sat Oct 05, 2013 11:58 pm
by tester
Because comma (",") is being considered in FS by some green prims as array separator and is automatically converted, here is a small tool to deal with strings understood as "custom text" (old way).

And by the way - found this one:
http://synthmaker.co.uk/forum/viewtopic.php?f=9&t=10354

;-)

Question: how to filter/clean (the old way, i.e. green prims) string from any non-numeric data in the prefix? (sufix is cut automatically as far I can see).

//edit:
had to remove 1 space in first modules in chain, to un-confuse small thing. Which is interesting, because the "space" - isn't it a part of the string too?

Re: replace words in a string

Posted: Sun Oct 06, 2013 12:31 pm
by trogluddite
The custom parser components are the most flexible 'primitive' way. Here's an example with some comments...
Parameter Parse.fsm
(2.61 KiB) Downloaded 1381 times

Custom parser components are only visible when "Show R&D" is selected in the advanced options, as they are tagged as "experimental". I've used them quite a bit, without any hint of instability etc., except for one small quirk - if you change the sequence of 'Rules', you sometimes have to break and re-make the first link (at the custom parser prim' 'rule' output), so that it reads the edits to the chain properly.
(PS - when searching the toolbox, search for "rule" to find the small primitives most easily)

It could be done other ways - e.g. scan the string with a loop to locate the first numeric character, then split the string. But if you have a lot of such strings to parse, the custom parser will be much more efficient, and less prone to trigger problems.

Re: replace words in a string

Posted: Sun Oct 06, 2013 2:19 pm
by tester
I was wondering where these parsers hide. Is there any info on how to use them?

Re: replace words in a string

Posted: Sun Oct 06, 2013 2:43 pm
by trogluddite
Sadly not - no mention in any of the documentation, though there were a few examples on the SM site.

Re: replace words in a string

Posted: Tue Oct 29, 2013 4:35 am
by strangeChild
There was a tutorial on the parsers in the WIKI that's now gone... I wonder if anyone we could get that back?

Re: replace words in a string

Posted: Tue Oct 29, 2013 12:56 pm
by stw