1.

Solve : find special characters?

Answer»

I am hopefull you guys can help me out with this question.

Do you know of a way to search a string of characters and find out if any are "special characters"?

example: if a string contains any of the following `[emailprotected]#$%^&*()_+-={}|[]\:";'<>?,./ then do [command] else [command]CHECK out the findstr command. Where does this string exist? If from a file you can check if not A-Z, and not a-z and not 0-9. If a character is not one mentioned, it must be a special character.

Good luck. Quote from: Sidewinder on May 12, 2008, 11:51:58 AM

Check out the findstr command. Where does this string exist? If from a file you can check if not A-Z, and not a-z and not 0-9. If a character is not one mentioned, it must be a special character.

Good luck.

This is the commands I am trying to run with some others to check for length and such. I have the string I am working with set as the varable choice.

rem checks to see if special used
set errorlevel=
echo %choice% |findstr /i /r "[^0-9]"
if /I %errorlevel% lss 1 goto spchar

it works but it also seems to be grabbing everything not just the special charactors.

any ideas?It seems the findstr command does not support multiple ranges in the regex expression.

The good news is there is always a way to build a better mousetrap. For this solution I had to reach deep into the DOS gene pool and then fast forward to the present.

Code: [Select]@echo off
set spechar=N
echo;;|choice /c=%choice%; set > p.bat
CALL p.bat
for %%v in (%[%) do call :check %%v
set [=
del p.bat > nul
if %spechar% equ Y echo This string: %choice% contains special characters
goto :eof


:check
echo %1 | findstr /r "[0-9]" > nul
if %errorlevel% EQU 0 goto :eof
echo %1 | findstr /r "[A-Z]" > nul
if %errorlevel% EQU 0 goto :eof
echo %1 | findstr /r "[a-z]" > nul
if %errorlevel% EQU 0 goto :eof
set spechar=Y

This works with the DOS version of choice not the NT version.I couldn't find a source for the choice command but you may have better luck with Google. You can also borrow a copy from another machine.

Good luck.
vbscript
Code: [Select]Set objArgs = WScript.Arguments
strToCheck = objArgs(0)
WScript.Echo strToCheck
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = False
objRE.Pattern = "[^a-z0-9]+"
Set colMatches = objRE.Execute(strToCheck)
For Each objMatch In colMatches
WScript.Echo "At position " & objMatch.FirstIndex & " matched " & objMatch.Value
WScript.Quit(2)
Next

usage: save the above as script.vbs and on command line :
Code: [Select]C:\test>cscript /nologo script.vbs "s^dfs"
s^dfs
At position 1 matched ^

C:\test>echo %errorlevel%
2
I have it working now but it is giving me some weird results

here is my code

Code: [Select]@echo off
GOTO itemB

:nowearly
cls
echo time not valid
echo please do not use between midnight and 1 AM
echo.
goto LOOPA

:spchar
cls
echo time not valid
echo please do not special charctors
echo.
goto itemB

:badtime
cls
echo time not valid
echo please do not add a : to the time
echo.
goto itemB

:badtimesc
cls
echo time not valid
echo please do not add a ; to the time
echo.
goto itemB

:badtimep
cls
echo time not valid
echo please do not add a . to the time
echo.
goto itemB

:badtimes
cls
echo time not valid
echo please make sure time is in 24 hour format
echo see example below
echo.
goto itemB

:badtimel
cls
echo time not valid
echo please do not use LETTERS in time
echo.
goto itemB

:tooearly
cls
echo time not valid
echo please do not use between midnight and 1 AM
echo.
goto itemB

:overtime
cls
echo time not valid
echo please do not set time greater then 2400
echo.
goto itemB

:minover
cls
echo time not valid
echo please do not set minutes greater then 60
echo.
goto itemB


:itemB
set spechar=N

rem gets user input and set a variable for the input
SET Choice=
echo Type the time in military (24 hour) format and press Enter:
echo Example: 1300 is 1:00 PM
SET /P Choice=time:

rem only accepts the first 4 characters entered
set choice=%choice:~0,4%

REM sets the number of characters to length
echo %choice%> %temp%\string.txt

REM get the file size in bytes
for %%a in (%temp%\string.txt) do set /a length=%%~za

REM do some batch arithmetic
set /a length -=3
if /i %length% lss 4 goto badtimes

REM clean up temp file
del %temp%\string.txt

rem sets the time to variables to be checked
set choice1h=%choice:~0,1%
set choice2h=%choice:~1,1%
set choice1m=%choice:~2,1%
set choice2m=%choice:~3,1%
set choicemm=%choice:~2,2%

goto check


REM checks for special charctors
:check

rem checks to see if a : is used
set errorlevel=
echo %choice% |findstr /i /r "[:]"
if /I %errorlevel% lss 1 goto badtime

rem checks to see if a ; is used
set errorlevel=
echo %choice% |findstr /i /r "[;]"
if /I %errorlevel% lss 1 goto badtimesc

rem checks to see if a . is used
set errorlevel=
echo %choice% |findstr /i /r "[.]"
if /I %errorlevel% lss 1 goto badtimep

echo %choice1h% | findstr /r "[0-9]" >nul
if %errorlevel% EQU 1 goto lettest

echo %choice2h% | findstr /r "[0-9]" >nul
if %errorlevel% EQU 1 goto lettest

echo %choice1m% | findstr /r "[0-9]" >nul
if %errorlevel% EQU 1 goto lettest

echo %choice2m% | findstr /r "[0-9]" >nul
if %errorlevel% EQU 1 (goto lettest) else goto timetest

:lettest
echo %choice% |findstr /i /r "[a-z]" >nul
if /I %errorlevel% lss 1 goto badtimel

echo %choice1h% |findstr /i /r "[a-z]" >nul
if /I %errorlevel% lss 1 goto badtimel

echo %choice2h% |findstr /i /r "[a-z]" >nul
if /I %errorlevel% lss 1 goto badtimel

echo %choice1m% |findstr /i /r "[a-z]" >nul
if /I %errorlevel% lss 1 goto badtimel

echo %choice2m% |findstr /i /r "[a-z]" >nul
if /I %errorlevel% lss 1 goto badtimel

:spcset

set spechar=Y

if %spechar% equ Y goto spchar

:timetest
rem validating time
if /I %choice% GEQ 2400 goto overtime
if /I %choicemm% GEQ 60 goto minover

rem makes sure that time is not set between midnight and 1 am
set choicea1=%choice:~0,2%
set choicea2=%choice:~0,3%
set errorlevel=
echo %choicea1% |findstr /i /r "/C:00"
if /I %errorlevel% lss 1 goto tooearly
echo %choicea1% |findstr /i /r "/C:000"
if /I %errorlevel% lss 1 goto tooearly

goto end

:end
echo done
pause
[\code]

if you place a ; in the 3rd number place it goes to the following section:

:badtimes
cls
echo time not valid
echo please make sure time is in 24 hour format
echo see example below
echo.
goto itemB


do you have any ideas as to why this is happening?Quote
It seems the findstr command does not support multiple ranges in the regex expression

In any other script language regular expressions can contain multiple ranges, so you can exclude upper and lower alphas and numerics and by the process of elimination conclude that at least one character is special.

Sending the entire string to the findstr command with a single range only proves you have upper, lower and numerics but tells you nothing one way or the other whether you have any special characters.

The choice command, deprecated in NT machines but brought back in Vista is quirky enough to allow you to parse a string into it's component characters. The logic posted assumed each character was not special, then checked each character separately. If the checks fell through all the logic, the flag was flipped indicating that at least one character was special.

Unless you want to do this with VBScript, you can download the choice command here.

Good luck.

There is a version of choice as a .exe file. Testing shows only the .com version of choice works as shown in my previous post.
wbrost, you need to be aware of a problem with set /a. It treats numbers with a leading zero as being octal and not decimal, which means that 08 and 09 will be rejected and there will be an error.

In my opinion batch language data validation sucks. You can break mosts scripts by inputting the '&' character, the so-called "poison character". Something like QBASIC is better.

Ghostdog will be along in a minute with a VBscript.



Quote from: Dias de verano on May 14, 2008, 12:41:23 PM
Ghostdog will be along in a minute with a VBscript.
already done. Post #4


Discussion

No Comment Found