【发布时间】:2017-03-28 20:42:31
【问题描述】:
我想更改一些字符串:
space+cows --> space + cows
stupid+rabbit --> stupid + rabbit
(put spaces around the `+`)
在 Sublime Text 2 中,我尝试使用这些:
Find: \w+\+\w+
Replace: \w+ \+ \w+
发现 regex 很好地匹配了所有内容,但显然,我的字符串被替换为字面意思w+ + w+。
再举一个例子:
Strings:
bool *foo --> bool* foo
int *bar --> int* bar
Pattern:
Find: (bool|int) *(foo|bar)
Replace: (bool|int)* (foo|bar)
Result:
(bool|int)* (foo|bar)
(bool|int)* (foo|bar)
不用说,我想保留原来的 bool、int、foo 和 bar。
我也不能只使用 \* 来匹配字符串,因为它会选择其他我不想替换的东西;我需要一些关于实际 \* 的上下文来选择正确的字符串。同样,我不能使用像 \*[^ ] 这样的模式,因为星号后面的 not-space 字符会在替换后被删除。
我通过使用 Sublime Text 的多行版本解决了我的问题,但我仍然想知道:是否可以使用 regex 来替换包含“字符组”的字符串而无需擦除“字符组”的实际内容?
【问题讨论】:
-
你可以在这里练习基本的正则表达式问题:regexone.com/lesson/introduction_abcs
-
那太好了,我去看看。谢谢!
标签: regex sublimetext2