China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-21 07:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to filter specified characters in a variable [Solved] View 8,220 Replies 31
Original Poster Posted 2006-08-30 21:41 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
The function I want to implement is to filter out specified characters or specify the type of characters used after the user enters any character. Please give instructions, experts!

[ Last edited by fornever on 2006-9-16 at 22:03 ]
Floor 2 Posted 2006-08-30 21:51 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Split the var into two parts. The first part is the first character, and the second part is all the remaining characters. Then judge whether the first character needs to be filtered... Then judge whether the remaining string is empty. If it is not empty, GOTO back to continue the loop.
Floor 3 Posted 2006-08-30 22:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re fornever:

What does it mean to filter out the input characters? Does it mean deleting the input characters in a certain text? I don't understand what operation you want to do.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-08-31 21:21 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
This is like, for example, using the command copy con h.txt or the set /p command to let the user input any characters, and then I filter out specified characters in the background, or it's not filtering, just prompt that which character is not allowed to be used. In short, I need to identify a certain kind of character! Oh, my Chinese is poor, please forgive me!~

re:NaturalJ0
Wondering how to split a string, including numbers, letters, and many symbols, or say, specifying that only a certain type of characters can be used, such as specifying that only letters and numbers can be used,
How to implement it?
Or rely on third-party tools?
Floor 5 Posted 2006-08-31 21:45 ·  中国 江苏 苏州 吴中区 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
set /p var="Please enter from user: "

%var:~0,1% This means the first character of variable var
%var:~1% This means all characters after the first character of variable var

After segmenting, you can do assignment or comparison, etc., and you can use this method to take the first character of the remaining string again. What to do next, I think you will know it yourself.
Floor 6 Posted 2006-08-31 22:23 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
Hehe. Just learned to check the reply before, but still thank you!
Batch processing is really profound, respect!
Think it's still write the code now:

set /p var=Please enter:
:start
if "%var:~0,1%"=="?" goto error
if "%var:~0,1%"==">" goto error
if "%var:~0,1%"=="<" goto error
if "%var:~0,1%"=="|" goto error
:: if "%var:~0,1%"=="Add filtered characters" goto error
if "var"=="" goto next
set var=%var:~1%
goto start
:next
echo succeed!
goto end
:error
echo error!
:end

Thank you again to the friends who helped!
Floor 7 Posted 2006-08-31 23:51 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Batch processing can't detect all characters. When it encounters sensitive characters like |, >, <, etc., it will automatically exit.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-09-01 12:55 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 9 Posted 2006-09-01 18:36 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
...Can the ^ symbol be used? Actually, my purpose is to filter out those characters that are not allowed to be used as file names
Floor 10 Posted 2006-09-04 05:19 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
Actually, it's still the previous question, but this time the difference is that we need to restrict the characters entered by the user after set /p to be numbers, uppercase and lowercase letters, and a few specific special symbols. Or it's also okay to restrict only one type!
Floor 11 Posted 2006-09-05 07:18 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
fingstr should be a typo of findstr.

Last time? Where was the last time? If it's not someone who cares, no one will pay attention to what post others posted last time. Hehe, your requirement makes me confused.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 12 Posted 2006-09-05 22:34 ·  中国 北京 鹏博士BGP
初级用户
Credits 98
Posts 35
Joined 2006-01-14 22:43
20-year member
UID 48986
Gender Male
Status Offline
I'm sorry, it's indeed
http://www.cn-dos.net/forum/viewthread.php?tid=22897&fpage=1&highlight=
The so-called last time

It means using if and findstr to judge whether there are restricted characters in the input after the user enters the string

I want to use findstr to restrict the data type of the input to uppercase and lowercase letters and numbers
code:

set /p input:
::Write the %input:% type restricted by findstr here
if ....
::This if statement judges whether there are disallowed character types, such as some symbols
Floor 13 Posted 2006-09-06 02:28 ·  中国 甘肃 张掖 临泽县 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 14 Posted 2006-09-06 02:53 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
findstr "^[a-z][A-Z]*$" Judge pure letters
findstr "^[0-9]*$" Judge pure numbers

@echo off
set/p aa=Enter your code:
echo %aa%|findstr "^[a-z][A-Z]*$"||echo ERROR!!!

Just wrote it casually, to judge pure letters, I wonder if the original poster means this???
我今后在论坛的目标就是做个超级坏人!!!
Floor 15 Posted 2006-09-06 02:57 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Here is the translation of the above content:

  Post a section of the usage of findstr. The original poster can imitate to write a code to restrict the input type:

Basic Usage of FINDSTR Regular Expressions

1. findstr . 2.txt or Findstr "." 2.txt
Find any characters from file 2.txt, excluding empty characters or blank lines
====================

2. findstr .* 2.txt or findstr ".*" 2.txt
Find any characters from file 2.txt including blank lines and empty characters
====================

3. findstr "" 2.txt
Find strings or lines including numbers 0-9 from file 2.txt
====================

4. findstr "" 2.txt
Find strings or lines including any characters from file 2.txt
====================

5. findstr "" 2.txt
Find strings or lines including letters a, b, c, e, z, y from file 2.txt
====================

6. findstr "" 2.txt
Find strings of lowercase characters a-f and l-z from file 2.txt, but do not include letters g, h, I, j, k.
====================

7. findstr "MY" 2.txt
Can match MahY, MbiY, MahY, etc. in file 2.txt…..
====================

8. Application of ^ and $ symbols
^ means the beginning of a line, " ^step" only matches the first word in "step hello world"
$ means the end of a line, "step$" only matches the last word in "hello world step"
====================

9. finstr "" 2.txt
If it is a pure digital string or line, filter it out. For example, a string like 2323423423, if it is in the form of 345hh888, it is not.
====================

10. findstr "" 2.txt
Same as above, if it is a pure letter string or line, filter it out. For example, a character like sdlfjlkjlksjdklfjlskdf, if it is in the form of sdfksjdkf99999, mixed with numbers, it is not.
====================

11. Role of * symbol
As mentioned earlier, ".*" means the search condition is any character. The role of * in regular expressions is not any character, but means the number of repetitions of the left character or expression. * means the number of repetitions is zero or more times.
====================

12. findstr "^*$" 2.txt
This matches pure numbers found, for example 234234234234, if it is 2133234kkjl234, it is filtered out.
Findstr "^*$" 2.txt
This matches pure letters found, for example sdfsdfsdfsdf, if it is 213sldjfkljsdlk, it is filtered out.
If there is no * sign in the search condition, that is, not repeating the left search condition, that is, , it can only match the first character of the string and only this character. Because of the restrictions of the beginning and end of the line, "^$" will match if the first character is a number, and filter out if it is not. If the string is 9, it matches; if it is 98 or 9j, etc., it is not possible.
=====================

13. The role of "\<…\>" expression
This means to accurately find a string. \<sss means the start position of a word, and sss\> means the end position of a word.
echo hello world computer|findstr "\<computer\>" such form
echo hello worldcomputer|findstr "\<computer\>" such form is not, it wants to find the string "computer", so it is not.
echo hello worldcomputer|findstr ".*computer\>" this can match
=====================
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: