TextMate:正则表达式用以下 0 替换 $1
我正在尝试修复一个充满 1 位数和 2 位数数字的文件,使它们全部为 2 位数长。
该文件的形式为:
10,5,2
2,4,5
7,7,12
...
我已经设法将问题编号与:
(^|,)(\d)(,|$)
我现在要做的就是将有问题的字符串替换为:
${1}0$2$3
但 TextMate 给了我:
10${1}05,2
有什么想法吗?
提前致谢, 罗斯
I'm trying to fix a file full of 1- and 2-digit numbers to make them all 2 digits long.
The file is of the form:
10,5,2
2,4,5
7,7,12
...
I've managed to match the problem numbers with:
(^|,)(\d)(,|$)
All I want to do now is replace the offending string with:
${1}0$2$3
but TextMate gives me:
10${1}05,2
Any ideas?
Thanks in advance,
Ross
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 this,TextMate 支持单词边界锚点,因此您也可以搜索
\ b\d\b
并将全部替换为0$0
。 (感谢 Peter Boughton 的建议!)这具有一次性捕获所有数字的优点 - 您的解决方案必须至少应用两次,因为正则表达式引擎在成功替换后已经消耗了下一个数字之前的逗号。
According to this, TextMate supports word boundary anchors, so you could also search for
\b\d\b
and replace all with0$0
. (Thanks to Peter Boughton for the suggestion!)This has the advantage of catching all the numbers in one go - your solution will have to be applied at least twice because the regex engine has already consumed the comma before the next number after a successful replace.
注意: Tim 的解决方案更简单并解决了这个问题,但我将其留在这里供参考,以防有人遇到类似但更复杂的问题,使用环视可以支持。
比表达式更简单的方法是替换:
With: ,
即“用 0 替换所有单个数字,然后再用它本身替换”。
正则表达式是:
负向后查找找不到数字
(?
一位数字:
\d
负向查找找不到数字
(?!\d)
Single 这是位置匹配(不是字符匹配),它同时满足逗号和开始/结束位置。
$0
部分表示“整个匹配” - 由于后向/前向匹配位置,这将包含匹配的单个数字。Note: Tim's solution is simpler and solves this problem, but I'll leave this here for reference, in case someone has a similar but more complex problem, which using lookarounds can support.
A simpler way than your expression is to replace:
With:
Which is "replace all single digits with 0 then itself".
The regex is:
Negative lookbehind to not find a digit
(?<!\d)
A single digit:
\d
Negative lookahead to not find a digit
(?!\d)
Single this is a positional match (not a character match), it caters for both comma and start/end positions.
The
$0
part says "entire match" - since the lookbehind/ahead match positions, this will contain the single digit that was matched.对于来到这里的任何人,正如 @Amargosh 建议的,这是一个错误,或者是故意的行为,如果没有其他原因,就会导致问题。
我刚刚遇到这个问题,不得不使用以下解决方法:如果您设置另一个捕获组,然后使用条件插入,它将起作用。例如,我有一个像这样的字符串,并且想用
02
替换01
,所以我捕获了主字符串$1
中的结束数字和$2
中的结束数字,这给了我一个看起来像(。
然后替换为
$1(?2:02)
。(?2:02)
是条件插入,在这种情况下总会找到一些东西,但为了解决在$n
。希望对某人有帮助。 这里有关于条件插入的文档To anyone coming here, as @Amarghosh suggested, it's a bug, or intentional behavior that leads to problems if nothing else.
I just had this problem and had to use the following workaround: If you set up another capture group, and then use a conditional insertion, it will work. For example, I had a string like
<WebObject name=Frage01
and wanted to replace the01
with02
, so I captured the main string in$1
and the end number in$2
, which gave me a regex that looked like(<WebObject name=(Frage|Antwort))(01)
.Then the replace was
$1(?2:02)
.The
(?2:02)
is the conditional insertion, and in this instance will always find something, but it was necessary in order to work around the odd conundrum of appending a number to the end of$n
. Hope that helps someone. There is documentation on the conditional insertion here在 TextMate 1.5.11 (1635)
${1}
中不起作用(如所描述的 OP)。我很欣赏有关更改查询字符串的许多建议,但是,如果您想在捕获组和数字之间进行中断,则有一个更简单的解决方案:
\u
。它是 TextMate 特定的替换语法,可将以下字符转换为大写。由于数字没有大写,因此它不执行任何操作并继续前进。 Tim Pietzcker 的回答的链接对此进行了描述。
就我而言,我必须清理 csv 文件,其中盒子尺寸以 cm x cm x mm 为单位。因此我必须在前两个数字中添加一个零。
关于超过10个捕获组的支持,我不知道这是否是一个bug。但正如 OP 和 @rossmcf 所写,
$10
被替换为null
。In TextMate 1.5.11 (1635)
${1}
does not work (like the OP described).I appreciate the many suggestions re altering the query string, however there is a much simpler solution, if you want to break between a capture group and a number:
\u
.It is a TextMate specific replacement syntax, that converts the following character to uppercase. As there is no uppercase for numbers, it does nothing and moves on. It is described in the link from Tim Pietzcker's answer.
In my case I had to clean up a csv file, where box measurements were given in cm x cm x mm. Thus I had to add a zero to the first two numbers.
Regarding the support of more than 10 capture groups, I do not know if this is a bug. But as OP and @rossmcf wrote,
$10
is replaced withnull
.您不需要
${1}
- 替换字符串最多仅支持九组 - 因此它不会将其误认为$10
。替换为
$10$2$3
You need not
${1}
- replace strings support only up to nine groups maximum - so it won't mistake it for$10
.Replace with
$10$2$3