正则表达式替换:一个匹配项中有多个替换项 [英] Regexp-replace: Multiple replacements within a match

查看:113
本文介绍了正则表达式替换:一个匹配项中有多个替换项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我们的MVC3项目转换为使用T4MVC.而且我想替换java-script include也可以与T4MVC一起使用.所以我需要更换

I'm converting our MVC3 project to use T4MVC. And I would like to replace java-script includes to work with T4MVC as well. So I need to replace

"~/Scripts/DataTables/TableTools/TableTools.min.js"
"~/Scripts/jquery-ui-1.8.24.min.js"

进入

Scripts.DataTables.TableTools.TableTools_min_js
Scripts.jquery_ui_1_8_24_min_js

此刻,我正在使用Notepad ++作为正则表达式工具,并且它正在使用POSIX正则表达式. 我可以找到脚本名称并将其替换为这些正则表达式:

I'm using Notepad++ as a regexp tool at the moment, and it is using POSIX regexps. I can find script name and replace it with these regexps:

查找:\("~/Scripts/(.*)"\)

替换为\(Scripts.\1\)

但是我不知道如何将文件名中的点和破折号替换为下划线,并将正斜杠替换为点.

But I can't figure out how do I replace dots and dashes in the file names into underscores and replace forward slashes into dots.

我可以以此来检查js文件名中的点号或破折号

I can check that js-filename have dot or dash in a name with this

 \("~/Scripts/(?=\.*)(?=\-*).*"\)

但是如何替换组中的组?

But how do I replace groups within a group?

需要在组内进行非贪婪替换,并且这些替换按顺序进行,因此正斜杠转换为点后将不会转换为下划线.

Need to have non-greedy replacement within group, and have these replacements going in an order, so forward slashes converted into a dot will not be converted to underscore afterwards.

这是一个非关键性的问题,我已经手动完成了所有替换,但是我认为我对regexp很满意,所以这个问题使我感到烦恼!

This is a non-critical problem, I've already done all the replacements manually, but I thought I'm good with regexp, so this problem bugs me!!

p.s.首选工具是Notepad ++,但是任何POSIX regexp解决方案都可以-)

p.s. preferred tool is Notepad++, but any POSIX regexp solution would do -)

p.p.s. 在这里您可以获取样品样本 这是目标文本

推荐答案

我只会使用 RegexHero

  1. 您可以将代码粘贴到目标字符串框中,然后将(?<=(~/Script).*)[.-](?=(.*"[)]"))放入Regular Expression框中,并在Replacement String框中放入_.

  1. You can past the code into the target string box, then place (?<=(~/Script).*)[.-](?=(.*"[)]")) into the Regular Expression box, with _ in the Replacement String box.

替换完成后,单击底部的Final String,然后选择Move to target string and start a new expression.

Once the replace is done, click on Final String at the bottom, and select Move to target string and start a new expression.

从此处,将(?<=(<script).*)("~/)(?=(.*[)]" ))|(?<=(Url.).*)(")(?=(.*(\)" )))粘贴到Regular Expression框中,并保留Replacement String框为空.

From there, Paste (?<=(<script).*)("~/)(?=(.*[)]" ))|(?<=(Url.).*)(")(?=(.*(\)" ))) into the Regular Expression box and leave the Replacement String box empty.

替换完成后,单击底部的Final String,然后选择Move to target string and start a new expression.

Once the replace is done, click on Final String at the bottom, and select Move to target string and start a new expression.

从此处将(?<=(Script).*)[/](?=(.*[)]"))粘贴到Regular Expression框中,然后将.粘贴到Replacement String框中.

From there paste (?<=(Script).*)[/](?=(.*[)]")) into the Regular Expression box and . into the Replacement String box.

之后,Final String框将显示您要查找的内容.我不确定您可以解析多少个文本的上限,但是如果这是个问题,可以将其分解.我相信可能会有更好的方法,但这往往是我处理此类事情的方法.我喜欢这个网站的原因之一是因为我不需要安装任何东西,因此我可以在任何地方快速安装它.

After that, the Final String box will have what you are looking for. I'm not sure the upper limits of how much text you can parse, but it could be broken up if that's an issue. I'm sure there might be better ways to do it, but this tends to be the way I go about things like this. One reason I like this site, is because I don't have to install anything, so I can do it anywhere quickly.

根据评论,我已将步骤3移至步骤5,并添加了新的步骤3和4.我必须这样做,因为新的步骤5会将"~/Scripts中的/替换为.,破坏了对"~/的删除.我还必须更改步骤5的代码以解决Script

Edit 1: Per the comments, I have moved step 3 to Step 5 and added new steps 3 and 4. I had to do it this way, because new Step 5 would have replaced the / in "~/Scripts with a ., breaking the removal of "~/. I also had to change Step 5's code to account for the changed beginning of Script

这篇关于正则表达式替换:一个匹配项中有多个替换项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆