我正在使用带有新“J”选项的REPL.BAT脚本,该选项允许使用JScript表达式替换参数。此脚本由@dbenham(原始REPL.BAT帖子和最早的StackOverflow帖子)创建。我有一个问题,我正在寻找一个模式,它正在取代我想要忽略的部分。
使用以下代码作为示例..
**1:** assertExpectedConditionTrue(driver, "By.NAME", "firewall_enabled")
**2:** assertExpectedConditionTrue(driver, "By.XPATH", "//span[@id='Save'], "Save")
我想在找到第一行代码的地方进行搜索/替换。然后我想用类似的代码行替换它,但是切换了方法的第二个和第三个参数。
assertExpectedConditionTrue(driver, "firewall_enabled", "By.NAME")
考虑到这一点,我创建了一个具有以下内容的脚本:
type "%%a" | repl "assertExpectedConditionTrue\(driver, (.*?), (.*?)\)" "assertExpectedConditionTrue(driver, $2, $1)" MX >"%%a.TEMP"
找到第一行代码并正确替换。问题是这个搜索/替换也找到第二行并将其更改为..
assertExpectedConditionTrue(driver, "//span[@id='Save'], "Save", "By.XPATH")
我明白这是在说..
$ 1 =“By.XPATH”
$ 2 =“// span [@ id ='保存'],”保存“
..正因为如此,它发现这一行是搜索/替换的有效候选者。我想知道的是如何让它忽略这一行。
为了记录,我将对此方法进行搜索/替换,它最多可包含5个参数。所以我需要进行3次搜索/替换('s),这将改变以下代码行。
**1:** assertExpectedConditionTrue(driver, arg1, arg2)
**2:** assertExpectedConditionTrue(driver, arg1, arg2, arg3)
**3:** assertExpectedConditionTrue(driver, arg1, arg2, arg3, arg4)
答案 0 :(得分:1)
大多数代码只是为了测试它。只需查看%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=$1$3$2$4$5"
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")
这应该替换保持代码格式。