简体   繁体   English

delims中的双引号=?

[英]Double quotes in delims=?

I'm very new to batch scripting, but in my last question I was trying to extract a link from a line of text, specifically:我对批处理脚本很陌生,但在我的最后一个问题中,我试图从一行文本中提取一个链接,特别是:

83:                   href="https://beepbeep.maxresdefault.rar"><img

What I want out of it is this:我想要的是这样的:

https://beepbeep.maxresdefault.rar

Someone suggested using for /f to separate the string, and I'd like to separate it every " mark, taking only the second token, as what I want is trapped between the two "" .有人建议使用for /f来分隔字符串,我想将每个"标记分开,只取第二个标记,因为我想要的被困在两个""之间。

Here is what I've got:这是我所拥有的:

for /f "delims=^" tokens=^2" %%G in (output2.txt) do @echo %%G %%H >> output3.txt

The batch crashes at this point, I'm guessing it's the wrong syntax, but I'm not sure where the issue is, maybe in the " part?此时批处理崩溃,我猜这是错误的语法,但我不确定问题出在哪里,也许在"部分?

Neat problem.整洁的问题。

I'd do it this way:我会这样做:

SETLOCAL ENABLEDELAYEDEXPANSION

for /F "tokens=2 delims=^>=" %%i in (output2.txt) do (
  set x=%%i
  set x=!x:"=!
  echo !x! >> output3.txt
)

Notes:笔记:

  • Instead of tokenising on the quote, I've tokenised on = (before) and > (after).我没有在报价上标记化,而是在= (之前)和> (之后)上标记化。 Because, as you already know, quotes are hard因为,正如您已经知道的那样,报价很难
  • I always do the delims last.我总是最后做delims。 Otherwise it might think the space between delims and tokens is a delimeter.否则它可能会认为分隔符和标记之间的空间是分隔符。
  • Then it uses the SET syntax that allows you to substitute one character for another to replace all occurances of the double quote with nothing.然后它使用SET语法,允许您用一个字符替换另一个字符,以将所有出现的双引号替换为空。
  • The SETLOCAL ENABLEDELAYEDEXPANSION is necessary because otherwise, each evaluation of the %x% in the loop uses the original value of %x% which is probably wrong. SETLOCAL ENABLEDELAYEDEXPANSION是必要的,因为否则,循环中%x%的每次评估都使用 %x %x%的原始值,这可能是错误的。 I always have this as the second line in my batch file.我总是将此作为批处理文件中的第二行。

Judging by how much you've already got, I'm guessing you've seen it, but if you haven't, I've found ss64.com to be the best resource.从你已经拥有多少来判断,我猜你已经看过了,但如果你还没有看过,我发现 ss64.com 是最好的资源。
https://ss64.com/nt/syntax-dequote.html https://ss64.com/nt/syntax-dequote.html

See how we delim it on double quotes, without surrounding quotes.看看我们delim在双引号上定界,而不用引号。 We have already assigned the variable between the quotes to %%a but if we did not, then to remove the double quotes from the string we expand the variable %%a to %%~a (see for /? on variable expansion):我们已经将引号之间的变量分配给%%a ,但如果我们没有,那么为了从字符串中删除双引号,我们将变量%%a扩展为%%~a (参见for /?关于变量扩展):

@for /f delims^=^"^ tokens^=2 %%a in (output2.txt) do @echo %%~a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM