Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 有选择地用正则表达式替换单词_Regex_Sublimetext3_Pcre - Fatal编程技术网

Regex 有选择地用正则表达式替换单词

Regex 有选择地用正则表达式替换单词,regex,sublimetext3,pcre,Regex,Sublimetext3,Pcre,正则表达式是否可以选择性地替换某些单词 我的文档包含以下几行内容: <type>xxx</type> 其中yyy=xxx,但改进应替换为增强,而新功能应替换为提案。在所有其他情况下,yyy应与xxx相同 直接的正则表达式将替换([^不可能将条件语句放在替换字符串中,也不可能将数据(不在字符串中)存储在模式本身中 升华文本更简单的方法显然是分几个步骤进行(在前面替换特殊字符串,在后面替换一般大小写)。好的方法是使用编程语言和xml解析器 但有可能用一个技巧一次完成全部替换

正则表达式是否可以选择性地替换某些单词

我的文档包含以下几行内容:

<type>xxx</type>
其中
yyy
=
xxx
,但
改进
应替换为
增强
,而
新功能
应替换为
提案
。在所有其他情况下,
yyy
应与
xxx
相同


直接的正则表达式将替换
([^不可能将条件语句放在替换字符串中,也不可能将数据(不在字符串中)存储在模式本身中

升华文本更简单的方法显然是分几个步骤进行(在前面替换特殊字符串,在后面替换一般大小写)。好的方法是使用编程语言和xml解析器

但有可能用一个技巧一次完成全部替换:

1) 在文件的最末尾添加此行(在新行中):

2) 使用此模式:

<type>(?|([^<]+)</type>(?=(?:.*\R)++#(?>[^:]+:[^#]+#)??\1:([^#]++).*#((.).*))|(([^<]+))</type>(?=(?:.*\R)++.*#((.).*)))|\R.*\z
$3
代表
“种类”:“
或无,
$2
代表
增强
提案
xxx
或无,
$4
代表
或无。)

3) 替换

想法很简单:将所有替换内容放入字符串本身,并在模式中使用分支重置
(?|.(..)。|.(..)
(使用此功能,每个备选方案中的捕获组具有相同的编号)。添加的行将自动删除

注意:如果您有两个以上的特殊术语需要替换,请填写最后一行(但
“种类”:“
必须保留在末尾),并用
*?
更改模式中的

图案详情:

<type>
(?|                        # open a branch reset group
                           # first branch: the special terms
    ([^<]+)                # capture the term in group 1
    </type>
    (?=                    # open a lookahead (nothing is consumed inside it)
        (?:.*\R)++ #       # reach the last line
        (?>[^:]+:[^#]+#)?? # skip a couple of term:repl if needed
        \1                 # until the content of group 1 is found
        : ([^#]++)         # capture the corresponding replacement
        .* #               # reach the last #
        ((.).*)            # capture '"kind":"' in group 3 and '"' in group 4
    )                      # close the lookahead
  |                        # OR second branch: the general case
    (([^<]+))              # capture the term in group 1 and 2
                           # (to have the same number than the previous branch)
    </type>
    (?=                    # open a lookahead
        (?:.*\R)++         # same thing than the previous branch
        .* #               # but this time only '"kind":"' and '"'
        ((.).*)            # are needed
    )
)                          # close the branch reset group
|                          # OR
\R.*\z                     # the last line (in this case all the
                           # groups are empty) 

(?|#打开分支重置组
#第一部分:特别条款
([^[^::]:[^#]+#)跳过两个术语:如果需要,请回复
\1#直到找到第1组的内容
:([^#]+)#捕获相应的替换
到达最后一站#
(().#捕获“种类”:“在第3组中”和“在第4组中”
)#关闭前视
|#或第二分支:一般情况
(([^…)
是一个原子组

++
*++
?++
是所有格量词


\z
是字符串结尾的锚点。

不,不可能用一个替换操作来完成。你很可能会通过变通方法得到答案。你使用什么语言?正则表达式语言是PCRE。我不是使用编程语言,只是使用高级文本编辑器来替换长代码文件。那么,t是什么呢ext editor?文本编辑器是崇高的文本,稳定的频道,构建3065。哇,这是一个非常好的解决方案。那里有几个操作符我需要了解更多,但这肯定是我开始学习的地方!非常感谢。只是一个简单的问题:
++
(?>)
??
是性能优化,分别与
+
(?:)
*?
达到相同的结果,对吗?@Peter:没有一般规则。例如
(?:..\R)+
确保以下内容在
(?:.*\R)时位于最后一行+因为回溯是可能的。
(或
*?
)是一种选择,我宁愿一对一对地前进,直到找到好的一对,而不是匹配所有的对,然后一对一对地回溯来找到它。
(?>…)
确实提高了性能,但主要目标是“移动”“一对一对地进行配对,而不考虑包含组的内容(使用
(?:…)
正则表达式引擎记录组中所有字符的每个位置,但不使用
(?>)
)@彼得:
??
只是
的非贪婪版本,
*?
*
的非贪婪版本。看看www.rexegg.com,它对贪婪/非贪婪、所有格量词/原子群等概念有很好的解释。。
<type>(?|([^<]+)</type>(?=(?:.*\R)++#(?>[^:]+:[^#]+#)??\1:([^#]++).*#((.).*))|(([^<]+))</type>(?=(?:.*\R)++.*#((.).*)))|\R.*\z
$3$2$4
<type>
(?|                        # open a branch reset group
                           # first branch: the special terms
    ([^<]+)                # capture the term in group 1
    </type>
    (?=                    # open a lookahead (nothing is consumed inside it)
        (?:.*\R)++ #       # reach the last line
        (?>[^:]+:[^#]+#)?? # skip a couple of term:repl if needed
        \1                 # until the content of group 1 is found
        : ([^#]++)         # capture the corresponding replacement
        .* #               # reach the last #
        ((.).*)            # capture '"kind":"' in group 3 and '"' in group 4
    )                      # close the lookahead
  |                        # OR second branch: the general case
    (([^<]+))              # capture the term in group 1 and 2
                           # (to have the same number than the previous branch)
    </type>
    (?=                    # open a lookahead
        (?:.*\R)++         # same thing than the previous branch
        .* #               # but this time only '"kind":"' and '"'
        ((.).*)            # are needed
    )
)                          # close the branch reset group
|                          # OR
\R.*\z                     # the last line (in this case all the
                           # groups are empty)