Extract leading numbers from a string in batch script

后端 未结 6 1639
灰色年华
灰色年华 2021-01-02 23:39

I am new to batch scripting. although i have shell and python solutions for this problem but got stuck in batch script.

I have string like 123_happy,

6条回答
  •  日久生厌
    2021-01-03 00:11

    Loop (1=>1000 should be enough ;)) on the characters of the variable and find the first char not in number range. Extract the substring before:

    @echo off
    
    set z=123_happy
    setlocal enabledelayedexpansion
    set result=
    
    for /L %%i in (0,1,1000) do (
    set zz=!z:~%%i,1!
    if x!zz!==x exit /b
    if !zz! lss 0  (
    set result=!z:~,%%i!
    goto out
    )
    if !zz! gtr 9  (
    set result=!z:~,%%i!
    goto out
    )
    )
    :out
    echo result=!result!
    

    result:

    result=123
    

提交回复
热议问题