Board logo

标题: 如何判断输入内容是什么类型的字符? [打印本页]

作者: namejm     时间: 2006-6-15 16:10    标题: 如何判断输入内容是什么类型的字符?

如果输入一行字符之后,要判断输入的究竟是不是纯字母、纯数字还是纯汉字,该如何解决?
作者: bagpipe     时间: 2006-6-16 10:45
namejm兄的想像力真是丰富啊,这样也能想的出来,这难道大了去了,判断一行字符是特殊符号,汉字还是字母,这个更是难上加难了,可以判断有很多种情况:
1 一行都是特殊符号
2 一行中都是汉字
3 一行中都是字母
4 一行中都是数字
5 一行中有符号+汉字
不写了,情况太多了,没有什么准确的判断对象,不好实现
作者: electronixtar     时间: 2006-6-16 11:10
提个思路,cscript+regex
作者: bagpipe     时间: 2006-6-16 11:14
我看也只能用正则表达式来进行搜索来完成这个任务了........我的妈呀,EST兄,你,你,你,你,你,你,已经是“银牌会员”了,OH, MY GOD!!!
作者: namejm     时间: 2006-6-16 13:39
  我想判断输入的内容是不是1,2,3或者M,T,W(星期的首字母)等日期格式,用在我的文件备份器中。
  最好是只用CMD里的命令来实现。
作者: bagpipe     时间: 2006-6-16 16:13
这个是判断一个输入是否存在:
@echo off
setlocal
set /p a=判断输入的字符是否在范围之内:
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 您输入的正确||echo 对不起同志,您输入日期或星期不存在
endlocal


这个是判断连续输入三个的判断,例如:判断 1,6,9是否正确

@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=判断输入的字符是否在范围之内:
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 您输入正确||echo 您输入

错误
)
endlocal


暂时是这样的情况,局限性太大了,不过看能否在简化或者增加判断的能力
作者: willsort     时间: 2006-6-17 02:31
Re namejm:

      findstr本身的正则表达式可以实现字符类型的检测。

      纯字母:findstr /r "^[a-zA-Z][a-zA-Z]*$"
      纯数字:findstr /r "^[0-9][0-9]*$"
      特定字母:findstr /r "^[MTW][MTW]*$"

      但考虑到你的具体应用,似乎可直接用at命令的执行成败来判断输入参数的合理性,当然它也存在无法指出多个参数中何者无效的缺陷。
作者: bagpipe     时间: 2006-6-17 08:45
willsort版主能否把FINDSTR命令对正则表达式的帮助贴出来,谢谢..........
作者: bagpipe     时间: 2006-6-17 09:03
FINDSTR

Search for strings in files.

Syntax
      FINDSTR [options] [/F:file] [/C:string] [/G:file] [string(s)] [pathname(s)]

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

[class]   Character class: any one character in set
[^class]  Inverse class: any one character not in set

[x-y]     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 "[0-9]"
If %ERRORLEVEL% EQU 0 echo The string contains one or more numeric characters

Echo 12G6 |FindStr /R "[^0-9]"
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