Discussion:
Removing non-alpha-numeric characters from a string
(too old to reply)
rashid19672008
2008-05-06 10:40:09 UTC
Permalink
How can I remove all non-alpha-numeric characters from a string? (i.e. only alpha-numerics should remain in the string).
tst
2008-05-06 10:40:10 UTC
Permalink
Like this?
 
<img src="http://forums.ni.com/attachments/ni/170/321317/1/Filter String.png">


Filter String.png:
http://forums.ni.com/attachments/ni/170/321317/1/Filter String.png
JB
2008-05-06 12:10:04 UTC
Permalink
Or even without a loop ?<img src="Loading Image..."> Extract from the help for the Search and Replace String function :Right-click the Search and Replace String function and select Regular Expression from the shortcut menu to configure the function for advanced regular expression searches and partial match substitution in the replacement string.Extract from the for the advanced search options :[a-zA-Z0-9] matches any lowercase or uppercase letter or any digit. You also can use a character class to match any character not in a given set by adding a caret (^) to the beginning of the class. For example [^a-zA-Z0-9] matches any character that is not a lowercase or uppercase letter and also not a digit.Message Edité par JB le 05-06-2008 01:49 PM


Example_VI_BD4.png:
http://forums.ni.com/attachments/ni/170/321330/1/Example_VI_BD4.png
tst
2008-05-06 13:10:06 UTC
Permalink
Yep, that looks better. I happily never had the need to use&nbsp;regexes too much, so the less I know about them the better. :smileywink:
JB
2008-05-06 13:40:07 UTC
Permalink
I'm ever again astonished by these string format specifiers ! So compact and powerful ! So easy... once the code works ! And at the same time so incomprehensible unless you are very familiar with them !Get File Extension.vi (Functions palette &gt;&gt; File I/O &gt;&gt; Advanced File Functions) with its \.[~\.]*$ format specifier is a good example.
tst
2008-05-06 13:40:09 UTC
Permalink
Yes, but I was always one of those who go with the feeling that languages like Perl look like an explosion in an ASCII factory. Having to decipher those hiroglyphics is not that much fun.
rashid19672008
2008-05-07 16:10:07 UTC
Permalink
Yup this worked for me! Thanks a lot, friend!
rashid19672008
2008-05-07 16:10:07 UTC
Permalink
Thanks a lot for the answer. One more query! How do you show the code pic on your post? (Do you upload your pics to some other image hoster and link to your post from there? If yes which image hoster do you recommend?)
smercurio_fc
2008-05-07 16:10:10 UTC
Permalink
rashid19672008 wrote:Thanks a lot for the answer.One more query! How do you show the code pic on your post? (Do you upload your pics to some other image hoster and link to your post from there? If yes which image hoster do you recommend?)I use <a href="http://forums.lavag.org/Code-Capture-Tool-file121.html&amp;s=597dac4781fef8fb5f73a68f2a637007" target="_blank">Code Capture Tool</a>. To place an image into your post, attach the file to your post. Submit the post, and then go back to edit the post. Right-click on the URL for the attachment and select "copy url" (or something like that). Then, place the cursor where you want the picture, click the picture icon in the toolbar above the message entry area and paste in the URL.
rashid19672008
2008-05-08 08:40:14 UTC
Permalink
Gee thanks a lot <a href="../view_profile?user.id=33243" target="top" class="auth_text" style="font-weight: bold;"> smercurio?</a> (again)! That was mighty useful info. Now I'll be able to bug you (good) guys with queries along with the pics. Sooo useful and makes life easy. Have a very nice day!
smercurio_fc
2008-05-06 15:40:06 UTC
Permalink
You folks should try to spend some time with bash scripts and grep. Your heads will start spinning, as mine has on multiple occasions. :smileyvery-happy:
altenbach
2008-05-06 15:40:08 UTC
Permalink
I would recommend to recruit the "lexical class" tool from the comparison palette for this, it can simplify your code quite a bit.
&nbsp;
You want lexical classes 3,4, and 5.
&nbsp;
Check the online help.
tst
2008-05-06 17:10:17 UTC
Permalink
altenbach wrote:
I would recommend to recruit the "lexical class" tool from the comparison palette for this, it can simplify your code quite a bit.


I thought about the lexical class primitive originally, but discarded the idea because it works on a single character.
&nbsp;
I never thought I would live to see the day where Altenbach suggests a loop (with a resizing operation, no less) as the simpler solution to a single primitive. :smileyvery-happy:
altenbach
2008-05-06 17:40:09 UTC
Permalink
tst wrote:


I never thought I would live to see the day where Altenbach suggests a loop (with a resizing operation, no less) as the simpler solution to a single primitive. :smileyvery-happy:



Hey!!! :D
&nbsp;
Yes, the loop can be done essentially in-place by re-using the input array with a little bit more code. Yes, the primitive is probably a&nbsp;more natural choice, but it requires fancy knowledge to compose the pattern string. ;)
&nbsp;
The purpose of my post was&nbsp;more educational&nbsp;to draw attention to the "lexical class" primitive. Most people have never seen it. :) It can be useful.
&nbsp;
Performance wise, the "search and replace all" is a multipurpose VI and the parsing and matching is probably more expensive than the lexical class, which could be implemented as a simple 8bit lookup table. I have not done any benchmarks though. :)
rashid19672008
2008-05-07 16:10:09 UTC
Permalink
Dear <a href="../view_profile?user.id=7614" target="top" class="auth_text" style="font-weight: bold;"> altenbach</a>! I'm left in the dark by your reply! What is lexical class? Do I need to install something to have access to it? What does it do?
altenbach
2008-05-07 16:10:14 UTC
Permalink
rashid19672008 wrote:What is lexical class?

It is a function that can be found in the comparison palette of LabVIEW.






rashid19672008 wrote:Do I need to install something to have access to it?

You need to have LabVIEW installed, nothing else.




rashid19672008 wrote:What does it do?

Once you found it, place it on the diagram and do a "right-click... help" on it. All clear? :D
rashid19672008
2008-05-08 09:10:08 UTC
Permalink
Gee! Thanks a lot for such a detailed answer <a href="../view_profile?user.id=7614" target="top" class="auth_text" style="font-weight: bold;"> altenbach</a>! And thanks again for introducing me to an overlooked member of labview functions palette. (I realized afterwards that you need to be an explorer to be real successful at LabVIEW (and everything for that matter). Have a very nice day!
craigc
2008-05-06 15:40:11 UTC
Permalink
Does that mean that once you have the Lexical class/classes you can filter strings with them?
Craig
altenbach
2008-05-06 15:40:12 UTC
Permalink
Here's a quick draft that would do the current problem.
&nbsp;
<img src="Loading Image..."> Message Edited by altenbach on 05-06-2008 08:34 AM


lexicalClass.png:
http://forums.ni.com/attachments/ni/170/321431/1/lexicalClass.png
craigc
2008-05-06 16:10:06 UTC
Permalink
Ahh Quite handy to know, Thanks
smercurio_fc
2008-05-06 17:40:10 UTC
Permalink
That's the same exact impression I had when I saw the example! Maybe he really, really dislikes regex expressions. :smileyvery-happy:
altenbach
2008-05-07 14:10:08 UTC
Permalink
smercurio_fc wrote:
Maybe he really, really dislikes regex expressions. :smileyvery-happy:


I think you are on to something here!
&nbsp;
It is true that I use these things rarely enought that I always need to consult the help screen. As soon as I need anything fancier, I never get things right on the first try. One reason I like LabVIEW is the fact that it is graphical and does not require any verbal syntax constructs. Any regex throws me back to the old text based code in some way. :D
smercurio_fc
2008-05-07 14:10:09 UTC
Permalink
:smileyvery-happy:&nbsp;:smileyvery-happy: That's exactly the same way I react whenever I have to use those regex expressions. I invariably have to crack open the full LabVIEW Help file to get those right.
Continue reading on narkive:
Search results for 'Removing non-alpha-numeric characters from a string' (Questions and Answers)
14
replies
Creating a "Why we should switch to Mac" Speech. Help Please?
started 2007-10-26 15:44:14 UTC
desktops
Loading...