Search user input for character

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Search user input for character

#1 Post by batchcc » 12 Feb 2016 07:21

I have the following code to get user input and I want to search their input for a character and set anything before that character as %before% the character as %char% and after that character as %after% does anyone have any idea on how to do this? Thanks everyone!

Code: Select all

@echo off
Cls
Set /p input="enter a word "
::Search for character
Set before=
Set char=
Set after=

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#2 Post by Squashman » 12 Feb 2016 07:40

You really have not provided enough detail or any examples of what the input and output would look like. What happens when their are multiple matches?

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Search user input for character

#3 Post by batchcc » 12 Feb 2016 08:37

If the user input has more than one of a character let's say "e" always use the first occurrence of the letter for example "there"

Code: Select all

Set before= th
Set char=e
Set after=re

The input will always be one word but may contain -^/+* and any number or lowercase letter

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#4 Post by Squashman » 12 Feb 2016 08:59

I use this function to find the zero based offset location of a string within another string.

Code: Select all

REM Subroutine to locate a string within another string and return the 0 based offset (-1 indicates not found)
:Instr [target-string] [search-string] [loc-var]
  set "%~3=-1" & set "_str1=%~1"
  call :StrLen "%~1" _len1 & call :StrLen "%~2" _len2 & set /A "_chk=_len1-_len2"
  for %%A in (!_len2!) do (for /L %%B in (0,1,!_chk!) do (if /I "%~2" == "!_str1:~%%B,%%A!" (set "%~3=%%B" & exit /b)))
  exit /b
 
 REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b


Use the CALL command to use it.

Code: Select all

call :InStr "there" "e" Loc


The CALL to the function will return the offset of the character you are looking for in the variable called Loc.

Once you know the position of the character you can then use the SET command to substring the input.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Search user input for character

#5 Post by batchcc » 12 Feb 2016 10:25

Every time I try this code even when the input has no "e" the variable %loc% echos -1

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#6 Post by Squashman » 12 Feb 2016 10:34

batchcc wrote:Every time I try this code even when the input has no "e" the variable %loc% echos -1

If it does not find the character it defaults to -1.

Please show us the code you are using. I am not going to guess at how you are trying to use the code.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Search user input for character

#7 Post by batchcc » 12 Feb 2016 10:37

loc.bat

Code: Select all

@echo off
cls
call :InStr "there" "e" Loc
echo %loc%
pause

REM Subroutine to locate a string within another string and return the 0 based offset (-1 indicates not found)
:Instr [target-string] [search-string] [loc-var]
  set "%~3=-1" & set "_str1=%~1"
  call :StrLen "%~1" _len1 & call :StrLen "%~2" _len2 & set /A "_chk=_len1-_len2"
  for %%A in (!_len2!) do (for /L %%B in (0,1,!_chk!) do (if /I "%~2" == "!_str1:~%%B,%%A!" (set "%~3=%%B" & exit /b)))
  exit /b
 
 REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#8 Post by Squashman » 12 Feb 2016 10:38

Did you notice all the variables using exclamation marks for expansion? That should tell you something.

You might want to put a GOTO :EOF before the functions in your batch file as well.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Search user input for character

#9 Post by Aacini » 12 Feb 2016 11:16

Code: Select all

@echo off
Cls
Set /p input="enter a word "

::Search for character "e"
Set "char=e"

for /F "tokens=1* delims=%char%" %%a in ("%input%") do (
   Set "before=%%a"
   Set "after=%%b"
)

echo Before: "%before%"
echo Char:   "%char%"
echo After:  "%after%"

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#10 Post by Squashman » 12 Feb 2016 11:47

Antonio +1!
I way over thought that.
K.I.S.S.

batchcc
Posts: 139
Joined: 17 Aug 2015 06:05
Contact:

Re: Search user input for character

#11 Post by batchcc » 13 Feb 2016 13:50

Thanks Aacini and Squashman, Aacini's code worked but just for my reference in the future if I wanted to ignore the first "e" and use the second occurrence of the letter "e" is there an easy way to do this as well? Thanks

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Search user input for character

#12 Post by Squashman » 13 Feb 2016 19:39

batchcc wrote:Thanks Aacini and Squashman, Aacini's code worked but just for my reference in the future if I wanted to ignore the first "e" and use the second occurrence of the letter "e" is there an easy way to do this as well? Thanks

It is as simple as using two tokens and using a, b, and c as your variables.

Code: Select all

@echo off
Cls
Set "input=therefore"

::Search for character "e"
Set "char=e"

for /F "tokens=1,2* delims=%char%" %%a in ("%input%") do (
   Set "part1=%%a"
   Set "part2=%%b"
   set "part3=%%c
)

echo Before: "%part1%%char%%part2%"
echo Char:   "%char%"
echo After:  "%part3%"

pause

output

Code: Select all

Before: "ther"
Char:   "e"
After:  "fore"
Press any key to continue . . .

Post Reply