replace words in a string

Post any examples or modules that you want to share here
Jay
Posts: 276
Joined: Tue Jul 13, 2010 5:42 pm

replace words in a string

Post 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 1473 times


best regards
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: replace words in a string

Post 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!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Jay
Posts: 276
Joined: Tue Jul 13, 2010 5:42 pm

Re: replace words in a string

Post 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
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: replace words in a string

Post 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!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: replace words in a string

Post 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?
Attachments
comma-else.fsm
(521 Bytes) Downloaded 1383 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: replace words in a string

Post 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 1380 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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
tester
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: replace words in a string

Post by tester »

I was wondering where these parsers hide. Is there any info on how to use them?
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: replace words in a string

Post by trogluddite »

Sadly not - no mention in any of the documentation, though there were a few examples on the SM site.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
strangeChild
Posts: 47
Joined: Sat Apr 27, 2013 8:04 pm

Re: replace words in a string

Post 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?
Post Reply