Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

replace words in a string

Post any examples or modules that you want to share here

replace words in a string

Postby Jay » Sat Jan 19, 2013 9:23 pm

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 1341 times


best regards
Jay
 
Posts: 276
Joined: Tue Jul 13, 2010 5:42 pm

Re: replace words in a string

Postby trogluddite » Sun Jan 20, 2013 4:18 pm

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

Re: replace words in a string

Postby Jay » Sun Jan 20, 2013 11:46 pm

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
Jay
 
Posts: 276
Joined: Tue Jul 13, 2010 5:42 pm

Re: replace words in a string

Postby trogluddite » Mon Jan 21, 2013 12:15 pm

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

Re: replace words in a string

Postby tester » Sat Oct 05, 2013 11:58 pm

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 1265 times
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: replace words in a string

Postby trogluddite » Sun Oct 06, 2013 12:31 pm

The custom parser components are the most flexible 'primitive' way. Here's an example with some comments...
Parameter Parse.fsm
(2.61 KiB) Downloaded 1267 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!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: replace words in a string

Postby tester » Sun Oct 06, 2013 2:19 pm

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.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: replace words in a string

Postby trogluddite » Sun Oct 06, 2013 2:43 pm

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

Re: replace words in a string

Postby strangeChild » Tue Oct 29, 2013 4:35 am

There was a tutorial on the parsers in the WIKI that's now gone... I wonder if anyone we could get that back?
strangeChild
 
Posts: 47
Joined: Sat Apr 27, 2013 8:04 pm

Re: replace words in a string

Postby stw » Tue Oct 29, 2013 12:56 pm

stw
 
Posts: 111
Joined: Tue Jul 13, 2010 11:09 am

Next

Return to User Examples

Who is online

Users browsing this forum: No registered users and 104 guests