Removing non alphanumeric characters in a batch variable

miestasmia

In batch, how would I remove all non alphanumeric (a-z,A-Z,0-9,_) characters from a variable?

I'm pretty sure I need to use findstr and a regex.

jeb

The solutionof MC ND works, but it's really slow (Needs ~1second for the small test sample).

This is caused by the echo "!_buf!"|findstr ... construct, as for each character the pipe creates two instances of cmd.exe and starts findstr.

But this can be solved also with pure batch.
Each character is tested if it is in the map variable

:test

    set "_input=Th""i\s&& is not good _maybe_???"
    set "_output="
    set "map=abcdefghijklmnopqrstuvwxyz 1234567890"

:loop
if not defined _input goto endLoop    
for /F "delims=*~ eol=*" %%C in ("!_input:~0,1!") do (
    if "!map:%%C=!" NEQ "!map!" set "_output=!_output!%%C"
)
set "_input=!_input:~1!"
    goto loop

:endLoop
    echo(!_output!

And it could be speed up when the goto loop is removed.
Then you need to calculate the stringLength first and iterate then with a FOR/L loop over each character.
This solution is ~6 times faster than the above method and ~40 times faster than the solution of MC ND

set "_input=Th""i\s&& is not good _maybe_!~*???"
set "_output="
set "map=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890"
%$strLen% len _input

for /L %%n in (0 1 %len%) DO (
    for /F "delims=*~ eol=*" %%C in ("!_input:~%%n,1!") do (
        if "!map:%%C=!" NEQ "!map!" set "_output=!_output!%%C"
    )
)
exit /b

The macro $strlen can be defined with

set LF=^


::Above 2 blank lines are required - do not remove
@set ^"\n=^^^%LF%%LF%^%LF%%LF%^^":::: StrLen pResult pString
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
        for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
            set "str=A!%%~2!"%\n%
              set "len=0"%\n%
              for /l %%A in (12,-1,0) do (%\n%
                set /a "len|=1<<%%A"%\n%
                for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
              )%\n%
              for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
        ) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing leading zeros from alphanumeric characters in R

From Dev

Removing non-alphanumeric characters with sed

From Dev

Batch Help: Removing Characters from a Variable with number inputs

From Dev

Removing non-alphanumeric characters without removing international characters in ruby

From Dev

Identifying special non alphanumeric characters in a string

From Dev

How to remove non alphanumeric characters and space, but keep foreign language in JavaScript

From Dev

Remove all non-alphanumeric characters, but keep diacritics (accents) and - (dash)

From Dev

How to ignore non alphanumeric characters for SQL LIKE statement?

From Dev

Non alphanumeric characters in R

From Dev

Split string on non alphanumeric characters using awk

From Dev

How to remove any non-alphanumeric characters?

From Dev

Replacing all non-alphanumeric characters except some characters

From Dev

Removing non alpha characters

From Dev

Finding records in Oracle DB with non-alphanumeric characters

From Dev

Strip file name of non-alphanumeric characters

From Dev

removing all non-printing characters by regex

From Dev

Removing spesific non-alphanumeric character from a string

From Dev

Removing non printable characters from expect logs?

From Dev

Removing non-alphanumeric characters from filenames and rename in Python

From Dev

Removing non alphanumeric characters in a batch variable

From Dev

Encode a string variable in non-alphanumeric order

From Dev

Regex to remove non-alphanumeric characters and all characters after dot?

From Dev

HTACCESS - Redirect with non alphanumeric characters

From Dev

Removing non-alphanumeric characters with bash or python

From Dev

cmd batch file removing characters fails

From Dev

Removing non-alphanumerics but maintain latin characters

From Dev

Remove non-alphanumeric characters by regex substitution

From Dev

Obtain a list from a string removing all non-alphanumeric characters

From Dev

Removing all non-ASCII characters

Related Related

  1. 1

    Removing leading zeros from alphanumeric characters in R

  2. 2

    Removing non-alphanumeric characters with sed

  3. 3

    Batch Help: Removing Characters from a Variable with number inputs

  4. 4

    Removing non-alphanumeric characters without removing international characters in ruby

  5. 5

    Identifying special non alphanumeric characters in a string

  6. 6

    How to remove non alphanumeric characters and space, but keep foreign language in JavaScript

  7. 7

    Remove all non-alphanumeric characters, but keep diacritics (accents) and - (dash)

  8. 8

    How to ignore non alphanumeric characters for SQL LIKE statement?

  9. 9

    Non alphanumeric characters in R

  10. 10

    Split string on non alphanumeric characters using awk

  11. 11

    How to remove any non-alphanumeric characters?

  12. 12

    Replacing all non-alphanumeric characters except some characters

  13. 13

    Removing non alpha characters

  14. 14

    Finding records in Oracle DB with non-alphanumeric characters

  15. 15

    Strip file name of non-alphanumeric characters

  16. 16

    removing all non-printing characters by regex

  17. 17

    Removing spesific non-alphanumeric character from a string

  18. 18

    Removing non printable characters from expect logs?

  19. 19

    Removing non-alphanumeric characters from filenames and rename in Python

  20. 20

    Removing non alphanumeric characters in a batch variable

  21. 21

    Encode a string variable in non-alphanumeric order

  22. 22

    Regex to remove non-alphanumeric characters and all characters after dot?

  23. 23

    HTACCESS - Redirect with non alphanumeric characters

  24. 24

    Removing non-alphanumeric characters with bash or python

  25. 25

    cmd batch file removing characters fails

  26. 26

    Removing non-alphanumerics but maintain latin characters

  27. 27

    Remove non-alphanumeric characters by regex substitution

  28. 28

    Obtain a list from a string removing all non-alphanumeric characters

  29. 29

    Removing all non-ASCII characters

HotTag

Archive