中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --

联盟域名:www.cn-dos.net  论坛域名:www.cn-dos.net/forum
DOS,代表着自由开放与发展,我们努力起来,学习FreeDOS和Linux的自由开放与GNU精神,共同创造和发展美好的自由与GNU GPL世界吧!

游客:  注册 | 登录 | 命令行 | 会员 | 搜索 | 上传 | 帮助 »
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » 如何判断输入内容是什么类型的字符?
作者:
标题: 如何判断输入内容是什么类型的字符? 上一主题 | 下一主题
namejm
荣誉版主

batch fan


积分 5226
发帖 1737
注册 2006-3-10
来自 成都
状态 离线
『楼 主』:  如何判断输入内容是什么类型的字符?

如果输入一行字符之后,要判断输入的究竟是不是纯字母、纯数字还是纯汉字,该如何解决?

2006-6-15 16:10
查看资料  发短消息 网志   编辑帖子  回复  引用回复
bagpipe
银牌会员

DOS联盟捡破烂的


积分 1144
发帖 425
注册 2005-10-20
来自 北京
状态 离线
『第 2 楼』:  

namejm兄的想像力真是丰富啊,这样也能想的出来,这难道大了去了,判断一行字符是特殊符号,汉字还是字母,这个更是难上加难了,可以判断有很多种情况:
1 一行都是特殊符号
2 一行中都是汉字
3 一行中都是字母
4 一行中都是数字
5 一行中有符号+汉字
不写了,情况太多了,没有什么准确的判断对象,不好实现

2006-6-16 10:45
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复
electronixtar
铂金会员





积分 7493
发帖 2672
注册 2005-9-2
状态 离线
『第 3 楼』:  

提个思路,cscript+regex




C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
2006-6-16 11:10
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
bagpipe
银牌会员

DOS联盟捡破烂的


积分 1144
发帖 425
注册 2005-10-20
来自 北京
状态 离线
『第 4 楼』:  

我看也只能用正则表达式来进行搜索来完成这个任务了........我的妈呀,EST兄,你,你,你,你,你,你,已经是“银牌会员”了,OH, MY GOD!!!

2006-6-16 11:14
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复
namejm
荣誉版主

batch fan


积分 5226
发帖 1737
注册 2006-3-10
来自 成都
状态 离线
『第 5 楼』:  

  我想判断输入的内容是不是1,2,3或者M,T,W(星期的首字母)等日期格式,用在我的文件备份器中。
  最好是只用CMD里的命令来实现。

2006-6-16 13:39
查看资料  发短消息 网志   编辑帖子  回复  引用回复
bagpipe
银牌会员

DOS联盟捡破烂的


积分 1144
发帖 425
注册 2005-10-20
来自 北京
状态 离线
『第 6 楼』:  

这个是判断一个输入是否存在:
@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


暂时是这样的情况,局限性太大了,不过看能否在简化或者增加判断的能力

2006-6-16 16:13
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复
willsort
元老会员

Batchinger


积分 4432
发帖 1512
注册 2002-10-18
状态 离线
『第 7 楼』:  

Re namejm:

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

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

      但考虑到你的具体应用,似乎可直接用at命令的执行成败来判断输入参数的合理性,当然它也存在无法指出多个参数中何者无效的缺陷。



※ Batchinger 致 Bat Fans:请访问 [讨论]批处理编程的异类 ,欢迎交流与共享批处理编程心得!
2006-6-17 02:31
查看资料  发送邮件  发短消息 网志   编辑帖子  回复  引用回复
bagpipe
银牌会员

DOS联盟捡破烂的


积分 1144
发帖 425
注册 2005-10-20
来自 北京
状态 离线
『第 8 楼』:  

willsort版主能否把FINDSTR命令对正则表达式的帮助贴出来,谢谢..........

2006-6-17 08:45
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复
bagpipe
银牌会员

DOS联盟捡破烂的


积分 1144
发帖 425
注册 2005-10-20
来自 北京
状态 离线
『第 9 楼』:  

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

2006-6-17 09:03
查看资料  发送邮件  访问主页  发短消息 网志   编辑帖子  回复  引用回复

请注意:您目前尚未注册或登录,请您注册登录以使用论坛的各项功能,例如发表和回复帖子等。


可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题



论坛跳转: