Skip to main content
Topic: How to strip everything but A-Z and 0-9 from a string ? but... (Read 155 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to strip everything but A-Z and 0-9 from a string ? but...

Hi,

I'm trying to strip non-alphabetical and non-numbers from string.

Usecase:

In ->File Operation->Move

I want to move selected music to another drive.

Like:
C:\music\A\Artist-beginning-with-A\...
C:\music\B\Artist-beginning-with-B\...
...
C:\music\Z\Artist-beginning-with-Z\...
C:\music\#\Artist-beginning-with-everything-which-is-not-a-letter\..

Every artist, which does not start with A-Z, will end up in the # folder.

This snippet will identify the folder where to put the artist.
Code: [Select]
$if($strchr(ABCDEFGHIJKLMNOPQRSTUVWXYZ,$put(F,$upper($ascii($cut(%ARTIST%,1))))),$get(F),#)

While this works great most of the time,
for artists, like: !!! or Økapi or İzel (but not for Μιλτιάδης Πασχαλίδης or Артек Электроника)

=> I don't want artists like ...But Alive to be in folder #, but in folder B.

I tried to be smart, with stripping/replacing/cutting everything which is not A-Z and 0-9, but I failed on the too many characters...
I even tried to trick out f2k to make a loop over a string, but I failed again.
Code: [Select]
$puts(Z,1)
$repeat(
$puts(Z,$add($get(Z),1))
$left(%ARTIST%,$get(Z))
,$len(%ARTIST))

So, how would I be able to:

strip any non-alphabetical and non-number character from the beginning of a string

until an alphabetical or number character shows up?

I'm sure, I always will have "problems" with artist like $tinkworx, but it will never be perfect...

Any idea/hint/code is much welcome! :)

Re: How to strip everything but A-Z and 0-9 from a string ? but...

Reply #1
Theoretically doable? Yes.
Way too complicated to actually use? Yes.

Proof of concept (finds first occurence of the character 'a' then cuts the part to the left):
Code: [Select]
$puts(mystring,%album artist%)
$puts(mylen,$len($get(mystring)))
$puts(mycut,$strchr($get(mystring),a))
$puts(mycalc,$add($sub($get(mylen),$get(mycut)),1))
$get(mycalc)

You'd need to expand line 3 to also check for characters 'ABCDEFGH....' etc and pick the lowest number out of all those (first occurence).

The code could also be simplified, but maybe is easier to understand this way.

Re: How to strip everything but A-Z and 0-9 from a string ? but...

Reply #2
This works for me, but it needs adjustments for non-ascii characters.

Code: [Select]
$puts(artist,$ascii($lower(%artist%)))
$puts(pos,$strchr($replace($get(artist),b,a,c,a,d,a,e,a,f,a,g,a,h,a,i,a,j,a,k,a,l,a,m,a,n,a,o,a,p,a,q,a,r,a,s,a,t,a,u,a,v,a,w,a,x,a,y,a,z,a,0,a,1,a,2,a,3,a,4,a,5,a,6,a,7,a,8,a,9,),a))
$right(%artist%,$sub($len(%artist%),$get(pos),-1))

If there are no special characters or special characters only, the original string is returned, otherwise special characters that precede the first non-special character are removed.






I'm late

 
SimplePortal 1.0.0 RC1 © 2008-2020