[英]Replace multiple lines with .net Regex
I am new to stackoverflow (my first post) and regex. 我是stackoverflow(我的第一篇文章)和regex的新手。 Currently i am working on a simple dirty app to replace baseclass properties with ctor injected fields.
目前,我正在开发一个简单的脏应用程序,以用ctor注入字段替换基类属性。 (cos i need to edit about 400 files)
(因为我需要编辑约400个文件)
It should find this: 它应该找到以下内容:
ClassName(WiredObjectRegistry registry) : base(registry)
{
and replace with: 并替换为:
ClassName(IDependency paramName, ISecondDependency secondParam, ... )
{
_fieldName = paramName;
...
so i need to replace the two old lines with three or more new lines. 所以我需要用三个或更多新行替换两条旧行。
basically i was thinking: 基本上我在想:
find this -> 找到这个->
className + ctorParams + zero or more whitespaces + newline + zero or more whitespaces + {
className + ctorParams +零个或多个空格+换行符+零个或多个空格+ {
replace with -> 替换为->
className + newCtorParams + newline + { my field assignments
className + newCtorParams +换行符+ {我的字段分配
i tried this regex for .net 我为.net尝试了此正则表达式
className + ctorParam + @"\w*" + "\r|\n" + @"\w*" + @"\{"
which does not replace the "{" and the whitespaces correctly 不能正确替换“ {”和空格
the replaced file content looks like this: 替换后的文件内容如下所示:
public CacheManager(ICallManager callManager, ITetraEventManager tetraEventManager, IConferenceManager conferenceManager, IAudioManager audioManager)
{
_callManager = callManager;
_tetraEventManager = tetraEventManager;
_conferenceManager = conferenceManager;
_audioManager = audioManager;
{
can u please help me with this :-| 你能帮我吗:-|
david 大卫
If you're translating 如果您要翻译
className + ctorParams + zero or more whitespaces + newline + zero or more whitespaces + {
into regex as 进入正则表达式为
className + ctorParam + @"\w*" + "\r|\n" + @"\w*" + @"\{"
then you're making several errors. 那么您就会犯几个错误。
First, the character class for whitespace is \\s
. 首先,空白字符类别为
\\s
。 \\w
means "alphanumeric character". \\w
表示“字母数字字符”。
Second, "\\r|\\n"
will result in the alternation operator |
其次,
"\\r|\\n"
将导致交替运算符|
separating the entire regex in two alternative parts (= "match either the regex before the |
or the regex after the |
"). 将整个正则表达式分成两个替代部分(=“匹配
|
之前的正则表达式或 |
之后的正则表达式)。 In your case, you don't need this bit at all since \\s
will already match spaces, tabs and newlines. 就您而言,您根本不需要此位,因为
\\s
已经可以匹配空格,制表符和换行符。 If you do want a regex that matches a Unix, Mac or DOS newline, use \\r?\\n?
如果您确实想要与Unix,Mac或DOS换行符匹配的正则表达式,请使用
\\r?\\n?
. 。
But, as the comments show, unless you show us what you really want to do, we can't help you further. 但是,正如评论所示,除非您向我们展示您真正想做的事,否则我们将无法为您提供进一步的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.