![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-15 12:31 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » How to determine what type of character the input content is? |
| Printable Version 3,461 / 8 |
| Floor1 namejm | Posted 2006-06-15 16:10 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
If you want to judge whether the input is pure letters, pure numbers or pure Chinese characters after entering a line of characters, how to solve it?
|
|
| Floor2 bagpipe | Posted 2006-06-16 10:45 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
Brother namejm's imagination is really rich! You can come up with this too. This is really amazing. Judging whether a line of characters is a special symbol, a Chinese character, or a letter is even more difficult. There can be many situations:
1. All are special symbols in a line 2. All are Chinese characters in a line 3. All are letters in a line 4. All are numbers in a line 5. There are symbols + Chinese characters in a line I won't write anymore. There are too many situations. There is no accurate judgment object, and it's not easy to implement |
|
| Floor3 electronixtar | Posted 2006-06-16 11:10 |
| 铂金会员 Posts 2,672 Credits 7,493 | |
|
Put forward an idea, cscript + regex
|
|
| Floor4 bagpipe | Posted 2006-06-16 11:14 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
I guess I can only use regular expressions for searching to complete this task........Oh my goodness, Brother EST, you, you, you, you, you, you have already become a "Silver Member", OH, MY GOD!!!
|
|
| Floor5 namejm | Posted 2006-06-16 13:39 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
I want to judge whether the entered content is a date format such as 1, 2, 3 or M, T, W (the first letters of the week), etc., and use it in my file backup tool. It is best to implement it only with commands in CMD.
|
|
| Floor6 bagpipe | Posted 2006-06-16 16:13 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
This is to judge whether an input exists:
@echo off setlocal set /p a=Judge whether the entered character is within the range: set b=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,M,T,W,Th,F,S,Su echo %b%|findstr /i "%a%" >nul&&echo Your input is correct||echo I'm sorry comrade, the date or week you entered does not exist endlocal This is to judge the judgment of continuous input of three, for example: judge whether 1,6,9 is correct @echo off setlocal set b=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,M,T,W,Th,F,S,Su set /p a=Judge whether the entered character is within the range: for /f "tokens=1,2,3 delims=," %%a in ("%a%") do ( echo %b%|findstr /i "%%a" >nul&&echo %b%|findstr /i "%%b" >nul&&echo %b%|findstr /i "%%c" >nul&&echo You input correctly||echo You input wrong ) endlocal This is the current situation, the limitation is too big, but see if it can be simplified or increase the judgment ability |
|
| Floor7 willsort | Posted 2006-06-17 02:31 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re namejm:
The regular expression of findstr itself can realize the detection of character types. Pure letters: findstr /r "^[a-zA-Z][a-zA-Z]*$" Pure numbers: findstr /r "^[0-9][0-9]*$" Specific letters: findstr /r "^[MTW][MTW]*$" But considering your specific application, it seems that the success or failure of the execution of the at command can be directly used to judge the rationality of the input parameters. Of course, it also has the defect that it cannot point out which one of the multiple parameters is invalid. |
|
| Floor8 bagpipe | Posted 2006-06-17 08:45 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
Moderator willsort, can you post the help for the FINDSTR command regarding regular expressions, please? Thanks..........
|
|
| Floor9 bagpipe | Posted 2006-06-17 09:03 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
FINDSTR
Search for strings in files. Syntax FINDSTR Key string Text to search for. pathname(s) The file(s) to search. /C:string Use string as a literal search string. /G:file Get search string from a file (/ stands for console). /F:file Get a list of pathname(s) from a file (/ stands for console). /d dirlist Search a comma-delimited list of directories. options may be any combination of the following switches: /I Case-insensitive search. /S Search subfolders. /P Skip any file that contains non-printable characters /L Use search string(s) literally. /R Use search string(s) as regular expressions.(default) /B Match pattern if at the Beginning of a line. /E Match pattern if at the END of a line. /X Print lines that match exactly. /V Print only lines that do NOT contain a match. /N Print the line number before each line that matches. /M Print only the filename if a file contains a match. /O Print character offset before each matching line. /a color_attribute Display filenames in colour (2 hex digits) Options in bold are new in Windows 2000When the search string contains multiple words (separated with spaces) then FINDSTR will show show lines that contains any one word - (an OR of each word) - this behaviour is reversed if the string argument is prefixed with /C. Regular Expressions (Searching for patterns of text) The FINDSTR syntax notation can use the following metacharacters which have special meaning either as an operator or delimiter. . Wildcard: any character * Repeat: zero or more occurances of previous character or class ^ Line position: beginning of line $ Line position: end of line Character class: any one character in set Inverse class: any one character not in set Range: any characters within the specified range \x Escape: literal use of metacharacter x \<xyz Word position: beginning of xyz\> Word position: end of word Metacharacters are most powerful when they are used together. For example, the combination of the wildcard character (.) and repeat (*) character is similar in effect to the filename wildcard (*.*) .* Match any string of charactersThe .* expression may be useful within a larger expression, for example f.*ing will match any string beginning with F and ending with ing. Examples: FINDSTR "granny Smith" MyFile.txt searches for "granny" OR "Smith" in MyFile.txt. FINDSTR /C:"granny Smith" MyFile.txt searches for "granny Smith" in MyFile.txt This is effectively the same as the FIND command To search every file in the current folder and all subfolders for the word "Smith", regardless of upper/lower case use: FINDSTR /s /i smith *.* Note that /S will only search below the current directory To find every line containing the word SMITH, preceeded by any number of spaces, and to prefix each line found with a consecutive number: FINDSTR /b /n /c:" *smith" MyFile.txt Finding a string only if surrounded by the standard delimiters To find the word "computer", but not the words "supercomputer" or "computerise": FINDSTR "\<computer\>" MyFile.txt Now assume you want to find not only the word "computer", but also any other words that begin with the letters comp, such as "computerise" or "compete" FINDSTR "\<comp.*" MyFile.txt Example of a literal search Searching a text file that contains the following the quick brown fox the darkbrown fox the really *brown* foxFINDSTR /r .*brown MyFile.txt or FINDSTR .*brown MyFile.txt Will both match the word "brown" in all 3 lines FINDSTR /L *brown* MyFile.txt Will only match the last string Using a script file Multiple search criteria can be specified with a script file /G. Multiple files to search can be specified with a source file /F. When preparing a source or script file, place each item on a new line. For example: to use the search criteria in CRIT.TXT and search the files listed in FILES.TXT then store the results in the file RESULTS.OUT, type FINDSTR /g:CRIT.TXT /f:FILES.TXT > results.out Errorlevel When an item is not found FINDSTR will return an errorlevel >0 Echo 12G6 |FindStr /R "" If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters Echo 12G6 |FindStr /R "" If %ERRORLEVEL% EQU 0 echo The string contains one or more non numeric characters Bugs In early versions of FindStr /F:file a path length of more than 80 chars will be truncated. "Twenty years from now, you will be more disappointed by the things you didn't do than by the ones you did do. So throw off the bowlines, sail away from the safe harbour. Catch the trade winds in your sails. Explore. Dream. Discover." - Mark Twain |
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |