cmd:如何提取部分字符串?

时间:2015-10-03 09:19:13

标签: batch-file cmd

在Windows命令行脚本中的

我有一个字符串变量示例:

AB_CD RR CRR LH 8AH_M22 1.1 2050_gra.pdf

这是一个文件名,我需要提取到另一个变量字符串(在这种情况下)2050(用户输入的时间)。

它始终由左侧至少一个空格(通常为三个)限制,并且始终由右侧的_gra.pdf限制。

它并不总是4位数字,有人可能会用点或逗号分隔小时和分钟。所以它看起来也像这样:

  • 20.50
  • 20,50
  • 20,50
  • 850
  • 8.50
  • 8,50
  • 0850
  • 08.50
  • 08,50

基本上是_gra.pdf与左侧最近空间之间的任何内容。

1 个答案:

答案 0 :(得分:4)

Var mystr = 'textarea text etc. ..';
 Var reg = RegExp='(^|\\.)([^\\.]*' + '(' + 'nokia' + '|' '    whatever' + '))+', 'g');
While(each = reg.exec(mystr)){
    Var sentence = each[0];
   // use the index position of the reg to relate to the position in the textarea
Console.log(reg.lastIndex);



}

编辑:满足“在_M和下一个空格之间提取任何内容”:

set "x=AB_CD RR CRR LH 8AH_M22 1.1 2050_gra.pdf"

REM get last element:
for %%i in ("%x: =" "%") do set last=%%i
rem split it by `_`:
for /f "Tokens=1 delims=_" %%i in (%last%) do set utim=%%~i
echo %utim%
相关问题