REPL.bat 替换我想单独留下的代码行的问题
Issue with REPL.bat replacing lines of code I want left alone
我正在使用带有新 'J' 选项的 REPL.BAT 脚本,它允许 JScript 表达式替换参数。此脚本由@dbenham 创建(原始 REPL.BAT post 和最早的 Whosebug post)。我在搜索模式时遇到问题,它正在替换我希望它忽略的部分。
以下面的代码为例..
**1:** assertExpectedConditionTrue(driver, "By.NAME", "firewall_enabled")
**2:** assertExpectedConditionTrue(driver, "By.XPATH", "//span[@id='Save'], "Save")
我想在找到第一行代码的地方做一个 search/replace。然后我想用类似的代码行替换它,但将方法的第二个和第三个参数调换。
assertExpectedConditionTrue(driver, "firewall_enabled", "By.NAME")
考虑到这一点,我创建了一个具有以下内容的脚本:
type "%%a" | repl "assertExpectedConditionTrue\(driver, (.*?), (.*?)\)" "assertExpectedConditionTrue(driver, , )" MX >"%%a.TEMP"
第一行代码找到并正确替换。问题是这个 search/replace 也在找到第二行并将其更改为..
assertExpectedConditionTrue(driver, "//span[@id='Save'], "Save", "By.XPATH")
我明白它在说..
$1 = "By.XPATH"
$2 = "//span[@id='Save'], "Save"
..因此,它发现此行是 search/replace 的有效候选者。我想知道的是如何让它忽略这一行。
郑重声明,我将为此方法做一个 search/replace,它最多可以包含 5 个参数。所以我需要制作 3 search/replace('s) 来更改以下代码行。
**1:** assertExpectedConditionTrue(driver, arg1, arg2)
**2:** assertExpectedConditionTrue(driver, arg1, arg2, arg3)
**3:** assertExpectedConditionTrue(driver, arg1, arg2, arg3, arg4)
大部分代码只是为了测试它。只需查看 %search%
和 %replace%
变量。
@echo off
setlocal enableextensions disabledelayedexpansion
Rem Prepare a file for testing
>inputFile.txt (
for /f "tokens=* delims=:" %%a in ('findstr /b ":::" "%~f0"') do echo(%%a
)
cls
type inputFile.txt
echo ------------------------------------------------------------------------------
rem Configure search / replace
set "search=(assertExpectedConditionTrue\(\s*driver)"
set "search=%search%(\s*,\s*(?:[^\q,\)]+|(?:\q[^\q]*\q)+))"
set "search=%search%(\s*,\s*(?!\q//)(?:[^\q,\)]+|(?:\q[^\q]*\q)+))"
set "search=%search%((?:\s*,\s*(?:[^\q,\)]+|(?:\q[^\q]*\q)+))*)"
set "search=%search%(\s*\))"
set "replace="
rem Execute the replacement operation
type inputFile.txt | repl "%search%" "%replace%" MX
rem Clean
del /q inputFile.txt
goto :eof
::: assertExpectedConditionTrue(driver, "By.NAME", "firewall_enabled")
::: assertExpectedConditionTrue(driver, "By.XPATH", "//span[@id='Save']", "Save")
::: assertExpectedConditionTrue(
::: driver,
::: "By.NAME",
::: "firewall_enabled"
::: )
::: assertExpectedConditionTrue(driver, "two", "three", four, "five")
::: assertExpectedConditionTrue(driver, two, "//three", four, "five")
::: assertExpectedConditionTrue(driver, "two", three, "four", "five")
这应该在保持代码格式的情况下进行替换。
我正在使用带有新 'J' 选项的 REPL.BAT 脚本,它允许 JScript 表达式替换参数。此脚本由@dbenham 创建(原始 REPL.BAT post 和最早的 Whosebug post)。我在搜索模式时遇到问题,它正在替换我希望它忽略的部分。
以下面的代码为例..
**1:** assertExpectedConditionTrue(driver, "By.NAME", "firewall_enabled")
**2:** assertExpectedConditionTrue(driver, "By.XPATH", "//span[@id='Save'], "Save")
我想在找到第一行代码的地方做一个 search/replace。然后我想用类似的代码行替换它,但将方法的第二个和第三个参数调换。
assertExpectedConditionTrue(driver, "firewall_enabled", "By.NAME")
考虑到这一点,我创建了一个具有以下内容的脚本:
type "%%a" | repl "assertExpectedConditionTrue\(driver, (.*?), (.*?)\)" "assertExpectedConditionTrue(driver, , )" MX >"%%a.TEMP"
第一行代码找到并正确替换。问题是这个 search/replace 也在找到第二行并将其更改为..
assertExpectedConditionTrue(driver, "//span[@id='Save'], "Save", "By.XPATH")
我明白它在说..
$1 = "By.XPATH"
$2 = "//span[@id='Save'], "Save"
..因此,它发现此行是 search/replace 的有效候选者。我想知道的是如何让它忽略这一行。
郑重声明,我将为此方法做一个 search/replace,它最多可以包含 5 个参数。所以我需要制作 3 search/replace('s) 来更改以下代码行。
**1:** assertExpectedConditionTrue(driver, arg1, arg2)
**2:** assertExpectedConditionTrue(driver, arg1, arg2, arg3)
**3:** assertExpectedConditionTrue(driver, arg1, arg2, arg3, arg4)
大部分代码只是为了测试它。只需查看 %search%
和 %replace%
变量。
@echo off
setlocal enableextensions disabledelayedexpansion
Rem Prepare a file for testing
>inputFile.txt (
for /f "tokens=* delims=:" %%a in ('findstr /b ":::" "%~f0"') do echo(%%a
)
cls
type inputFile.txt
echo ------------------------------------------------------------------------------
rem Configure search / replace
set "search=(assertExpectedConditionTrue\(\s*driver)"
set "search=%search%(\s*,\s*(?:[^\q,\)]+|(?:\q[^\q]*\q)+))"
set "search=%search%(\s*,\s*(?!\q//)(?:[^\q,\)]+|(?:\q[^\q]*\q)+))"
set "search=%search%((?:\s*,\s*(?:[^\q,\)]+|(?:\q[^\q]*\q)+))*)"
set "search=%search%(\s*\))"
set "replace="
rem Execute the replacement operation
type inputFile.txt | repl "%search%" "%replace%" MX
rem Clean
del /q inputFile.txt
goto :eof
::: assertExpectedConditionTrue(driver, "By.NAME", "firewall_enabled")
::: assertExpectedConditionTrue(driver, "By.XPATH", "//span[@id='Save']", "Save")
::: assertExpectedConditionTrue(
::: driver,
::: "By.NAME",
::: "firewall_enabled"
::: )
::: assertExpectedConditionTrue(driver, "two", "three", four, "five")
::: assertExpectedConditionTrue(driver, two, "//three", four, "five")
::: assertExpectedConditionTrue(driver, "two", three, "four", "five")
这应该在保持代码格式的情况下进行替换。