批处理:如何存储在正则表达式搜索和查找过程中遇到的值替换(替换蝙蝠) [英] Batch: How to store values encountered during a regular expression search & replace (repl.bat)
问题描述
使用批处理,我正在寻找一种方法(1)搜索正则表达式模式,以及(2)在该模式中存储变量...以便可以在搜索和替换时使用这些变量.
Using Batch, I'm looking for a way to (1) search for a regular expression pattern and (2) store variables within that pattern... so I can use those variables while doing a search and replace.
我一直在使用REPL.BAT,它是由 @dbenham (最早的StackOverflow帖子找到并替换文件中的文本) .这是这种情况的示例.
I've been using REPL.BAT, which was created by @dbenham (original REPL.BAT post and earliest StackOverflow post). Here's an example of the scenario.
我在文档中搜索以下代码的出现:
I search a document for the occurence of the following code:
driver.find_element_by_id("username").send_keys("rwbyrd")
driver.find_element_by_id("password").send_keys("password")
并替换为...
login("rwbyrd","password")
使用以下批处理代码:
type %filename% | repl "\sdriver\.find_element_by_id\(\x22username\x22\)\.send_keys\(\x22rwbyrd\x22\)\s*driver\.find_element_by_id\(\x22password\x22\)\.send_keys\(\x22password\x22\)" "login(\x22rwbyrd\x22,\x22password\x22)" MX >%filename%.new
注意:\ x22 =十六进制的双引号
NOTE: \x22 = Hex for double quotes
我正在寻找一种存储我正在搜索&的代码中的[用户名]'rwbyrd'和[密码]'密码'值的方法.进行替换时使用这些值.我想找到一种在REPL.BAT命令中实现此目标的方法,但我将尽我所能:).有谁知道如何实现这一目标?提前谢谢!
What I am looking for is a way to store the [username] 'rwbyrd' and [password] 'password' values from the code I'm searching for & use those values when doing a replace. I'd like to find a way to achieve this within the REPL.BAT command, but I will take what I can get :). Does anyone know how to achieve this? Thanks ahead of time!
推荐答案
如果尚未完成此操作,强烈建议您从命令提示符处执行以下命令:
If you haven't done this already, I strongly recommend you execute the following commands from the command prompt:
要获得特定于REPL.BAT的帮助,请执行以下操作:
To get help specific to REPL.BAT:
repl /?
获得有关REPL.BAT使用的JScript正则表达式的帮助(在Web浏览器中打开MicroSoft文档)
To get help about the JScript regular expressions used by REPL.BAT (opens up MicroSoft documentation in your web browser)
repl /?regex
要获取有关替换子匹配功能(正在寻找的变量")的帮助(在Web浏览器中打开MicroSoft文档)
To get help about the replace submatch features - the "variables" you are looking for (opens up MicroSoft documentation in your web browser)
repl /?replace
我在搜索正则表达式中用\b
(单词边界)替换了前导\s
.我还使用未转义的括号捕获用户名和密码值,以便可以在替换字符串中使用它们. .*?
与括号文字中的内容匹配(非贪婪匹配).一种外观上的更改是使用\q
而不是\x22
.
I replaced the leading \s
with \b
(word boundry) in your search regex. I also use un-escaped parentheses to capture the username and password values so that I can use them in the replacement string. The .*?
matches the content within the parentheses literals (non-greedy match). One cosmetic change was to use \q
instead of \x22
.
type %filename% | repl "\bdriver\.find_element_by_id\(\qusername\q\)\.send_keys\((.*?)\)\s*driver\.find_element_by_id\(\qpassword\q\)\.send_keys\((.*?)\)" "login($1,$2)" MX >%filename%.new
更新
根据OP的评论和Aacini的回答,我更新了REPL.BAT以接受J选项,以将替换值指定为JScript表达式.
Based on OP's comment, and Aacini's answer, I've updated REPL.BAT to accept a J option to specify the replacement value as a JScript expression.
以下示例将用户名转换为大写:
The following example will convert the username to upper case:
type %filename% | repl "\bdriver\.find_element_by_id\(\qusername\q\)\.send_keys\((.*?)\)\s*driver\.find_element_by_id\(\qpassword\q\)\.send_keys\((.*?)\)" "'login('+$[1].toUpperCase()+','+$[2]+')'" MXJ >%filename%.new
请参见 http://www.dostips.com/forum/viewtopic .php?p = 37855#p37855 了解详细信息和示例.
See http://www.dostips.com/forum/viewtopic.php?p=37855#p37855 for details and examples.
这篇关于批处理:如何存储在正则表达式搜索和查找过程中遇到的值替换(替换蝙蝠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!