首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Notepad++ RegEx帮助-从这里替换到另一个

Notepad++ RegEx帮助-从这里替换到另一个
EN

Stack Overflow用户
提问于 2014-08-27 00:29:14
回答 1查看 66关注 0票数 1

好吧,是这样的:

代码语言:javascript
复制
 I'm writing a huge programming file, and, while near the end, I realize that I made a stupid mistake. Basically, the text looks like this now:  
代码语言:javascript
复制
"usable_by"  
{  
    //Here is a line(s) that looks like the following:  
    "one" "1"  
    "two" "1"  
    //It can have any combination of these lines, with "one" or "two" in the lines above going up to nine.  
}  

评论显然不是程序的一部分,是你的笔记,以帮助我。

我希望将这些实例替换为所有九个属性,因此如下所示:

代码语言:javascript
复制
{
    "one" "1"
    ...
    "nine" "1"
}

和..。是2-8岁。

我猜您将不得不设计一种方法来替换{和}之间的任何东西(但只在"usable_by“标记上,以便其他标记不会被破坏)。

任何帮助都是非常感谢的。此外,我希望你能解释它,而不是仅仅给我这条正则线可能是什么。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-27 01:11:48

拜托,拜托,在运行之前备份一下你的资料。它对我有效,但我只有一点点的样本文本。

找出什么:

代码语言:javascript
复制
("usable_by"\s*\{\s*\n)(\s*)[^}]*\n(\s*)

代之以:

代码语言:javascript
复制
\1\2"one" "1"\n\2"two" "1"\n\2"three" "1"\n\2"four" "1"\n\2"five" "1"\n\2"six" "1"\n\2"seven" "1"\n\2"eight" "1"\n\2"nine" "1"\n\3

Regex细分:

代码语言:javascript
复制
(              # Start of first capturing group. Save for use in the replacement.
  "usable_by"  # This should be obvious.
  \s*          # \s match any whitespace character, the * means "zero or more."
  \{           # The opening curly brace.
  \s*          # More optional additional space.
  \n           # Newline.
)              # End of first capturing group.

(\s*)          # Second capturing group. Grabs the attribute indentation.

[^}]*          # Everything up to the closing curly brace.
               # Except it'll backtrack a bit so we can get rid of the next two.
\n             # Newline, the last before your final } line.

(\s*)          # Third capturing group. Picks up the indentation
               # leading up to the closing curly brace.

更换细目:

代码语言:javascript
复制
\1             # Back-reference to the first capturing group. Puts it here.
\2             # And the second, your indentation. We'll reuse this a bunch.
"one" "1"      # Your first attribute.
\n             # A newline

\2             # There's your indentation again.
"two" "1"      # And second attribute.
\n             # And newline.

...            # etc. don't really need to explain them all individually.

\2"nine" "1"\n # Indentation, ninth attribute, newline.
               # You're used to all this by now.

\3             # The third capturing group. The indentation leading
               # up to your closing }.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25517151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档