Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:27:54+00:00 2026-06-04T09:27:54+00:00

I am trying to understand a batch file that was sent to me in

  • 0

I am trying to understand a batch file that was sent to me in order to work around a bug in a third party program while they resolve the issue. Basically they are running a findstr regular expression command in order to determine whether or not the string matches. If it does, then the special characters that should not be stripped out are being added back in manually before it is passed off to the original commandline program.

As best I can tell though, what has been provided does not work or I do not understand it. I am pasting the relevant section of code below.

@echo off
setlocal
set username=%1
shift
echo %username% | findstr /r "^\"[0-9][0-9]*\"" >nul
if not errorlevel 1 (set username=";%username:~0,9%=%username:~10,4%?")
echo %username%

The three pieces I really have questions about are as follows:

  1. I believe the unescaped interpretation of the regular express above is ^"[0-9][0-9]*" which I think means that the string must begin with a numeric character and then must consist of zero or more additional numeric-only characters in order for a match to be found. Well, FINDSTR seems to be doing something weird with the escaped quotes and I cannot get it to match anything I have tried. If I remove the \" around [0-9][0-9]* then I can get it to work, but it does not properly reject non-numeric characters such as an input string of 123456789O1234 (there is a letter O instead of a zero in that sample string).
  2. What is the point of the >nul
  3. Wouldn’t it be better to check for an errorlevel equal to 0 instead of “not errorlevel 1” since it could possibly return an error level of 2?

Anyway, the following code works, but it is not as precise as I would like. I am just looking to understand why the quotes in the regex string are not working. Perhaps this is a limitation of FINDSTR, but I have not came across anything definitive yet.

@echo off
setlocal
set username=%1
shift
echo %username% | findstr /r "^[0-9][0-9]*" >nul
if not errorlevel 1 (set username=";%username:~0,9%=%username:~10,4%?")
echo %username%

I can workaround the problem by repeating the class 14 times since that is the number of characters in my situation (more than 15 classes will cause it to crash – scroll to the bottom). I am still curious as to how this could be achieved more simply, and of course the remaining 2 questions.

EDIT / WORKING SOLUTION

@echo off
setlocal enableDelayedExpansion
set username=%~1
shift
echo !username!|findstr /r /c:"^[0-9][0-9]*$" >nul 
if not errorlevel 1 (set username=";!username:~0,9!=!username:~10,4!?")
echo !username!

NOTES:

  • When I first ran it after modifying my existing code to more cloesly resemble dbenham’s, enableDelayedExpansion gave an error as did the quotes around setting the username (see below). I can’t replicate what I did wrong, but it is all working now (this is in case someone else comes across the same issue).
  • I had tried the $ for the EOL marker (which is the key to forcing it match numeric content only), but I think that the other problems were getting in the way which made me think it was not the solution. Also, to ensure the $ works don’t miss this part of dbenham’s answer “…you must also make sure there are no spaces between your echoed value and the pipe symbol.”
  • In short it pretty much seems that trying to put double quotes inside a regex for findstr is wrong syntax/does not work/etc… unless you are actually looking to match ” in the string/files you are parsing through. See dbenham’s answer for clarity here. As he noted, you can use %~1 to strip the quotes from the argument instead of adding it to your regex (and programmatically add them back in if needed).

Error Message

C:>sample.bat 123456789
'enableDelayedExpansion' is not recognized as an internal or external command,
operable program or batch file.
'"' is not recognized as an internal or external command,
operable program or batch file.
!username!

Reference Links:

  • Undocumented features and limitations of the Windows FINDSTR command
  • Case sesntive anomalies with findstr (not handling case properly in some circumstances)
  • http://ss64.com/nt/findstr.html
  • http://www.robvanderwoude.com/findstr.php
  • http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/findstr.mspx
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:27:55+00:00Added an answer on June 4, 2026 at 9:27 am

    Answering your questions in reverse order:

    3) if not errorlevel 1 is probably the same as if %errorlevel%==0 because IF ERRORLEVEL 1 means if ERRORLEVEL is greater than or equal to 1. So putting a NOT in front means if ERRORLEVEL is less than 1. I believe FINDSTR never returns a negative ERRORLEVEL, so the syntax should be OK.

    2) The >nul redirects the stdout output of FINDSTR to the nul device, meaning it disables the output. Normally any matching line would be printed. You are only interested in the return code – you don’t want to see the output.

    1) The original regex will match any input string that starts with a quote, followed by at least one digit, followed by another quote. It ignores any characters that may appear after the 2nd quote.

    So the following strings (quotes included) will match:

    • “0”
    • “01234”
    • “0”a
    • “01234”a

    The following strings will not match:

    • 0
    • 01234
    • “”
    • “0a”

    The original code has problems if the number of digits in the matching string reaches a certain length because the ending quote gets stripped causing the closing ) to be quoted and so the rest of the script fails.

    I don’t understand your requirements so I don’t know how to fix the code.

    It sounds like you don’t want to match strings that have non digits. That means you need to include the end of line marker $ at the end of the regex. But you must also make sure there are no spaces between your echoed value and the pipe symbol.

    I believe you probably don’t want quotes in your value, (or else you should programatically add them at the very end). You can use %~1 to strip any enclosing quotes from the supplied argument.

    If you are looking to check if argument 1 consists of nothing but numeric digits, then you can use:

    setlocal enableDelayedExpansion
    set "username=%~1"
    echo !username!|findstr /r "^[0-9][0-9]*$" >nul
    

    I used delayed expansion because you have no control over what characters are in %1, and if it contains special characters like & or | it will cause problems if you use normal expansion. The syntax I have given is not bullet proof, but it handles most “normal” situations.

    It is not necessary in your case, but I prefer to use the /c option, just in case your search string contains spaces. So the above could be written as

    echo !username!|findstr /r /c:"^[0-9][0-9]*$" >nul
    

    It seems odd to me that both the original and your modified code simply pass through the username if it does not match your regex. Maybe that is your intent, maybe not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying understand how multi queries work in mysqli. But I confess that
I am trying to understand a Windows batch file with the following statements: :GetVSCommonToolsDirHelper32
I'm trying to understand why one would use Spring Batch over a scripting language
Just trying to understand that - I have never used it before. How is
Trying to understand, why my C++/Qt application creates 18 threads, while i don't create
Trying to understand something that I don't know how to describe because I don't
trying to understand http and headers i was playing around with telnet to send
Trying to understand this open source app on github, it has a gem file:
I'm trying to understand a problem I have run into that I don't believe
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.