方案一,通过变量替换不断去除 href 关键词之前的内容,再以双引号为分隔符取地址,但需要预设固定的循环次数(此处为10),处理含有较多 href 的行时可能会漏:- @echo off
- for /f "delims=" %%a in ('findstr /ic:" href" a.txt') do (
- endlocal
- set str=%%a
- setlocal enabledelayedexpansion
- for %%b in (1 1 10) do (
- set str=!str:* herf=!
- for /f tokens^=2-3delims^=^": %%c in ("!str!") do (
- endlocal
- if /i %%c==http echo http:%%d
- setlocal enabledelayedexpansion
- )
- )
- )
- pause
复制代码 方案二,利用 = 为 for 默认分隔符的特性,将每行内容扔进 for 中直接循环并取地址,但不兼容网址含 * 的情况:- @echo off
- for /f "delims=" %%a in ('findstr /ic:" href" a.txt') do (
- set str=%%a
- setlocal enabledelayedexpansion
- for %%b in (!str:?^=\!) do (
- endlocal
- for /f "tokens=1* delims=:" %%c in ("%%~b") do (
- if /i %%c==http (
- set url=http:%%d
- setlocal enabledelayedexpansion
- echo !url:\=?!
- )
- )
- )
- )
- pause
复制代码
|