正则表达式正在砍掉文件名的最后一个字符

时间:2022-03-23 00:24:57

Anyone know why this is happening:

有人知道为什么会这样:

Filename:     031\_Lobby.jpg

RegExp:       (\d+)\_(.*)[^\_e|\_i]\.jpg

Replacement:  \1\_\2\_i.jpg

That produces this:

生产:

031\_Lobb\_i.jpg

For some reason it's chopping the last character from the second back- reference (the "y" in "Lobby". It doesn't do that when I remove the [^_e|_i] so I must be doing something wrong that's related to that.

由于某种原因,它将最后一个字符从第二个引用(“大厅”中的“y”)中删除。没有,当我把[^ _e | _i]所以我一定是做错了什么相关的。

Thanks!

谢谢!

3 个解决方案

#1


4  

You force it to chop off the last character with this part of your regex:

你强迫它用你的regex的这一部分砍掉最后一个字符:

[^_e|_i]

Which translates as: Any single character except "_", "e", "|", "i".

它的意思是:除了“_”、“e”、“|”、“i”之外的任何单个字符。

The "y" in "Lobby" matches this criterion.

“大厅”中的“y”符合这一标准。

You mean "not _e" and "not _i", obviously, but that's not the way to express it. This would be right:

你的意思显然是"not _e"和"not _i",但那不是表达的方式。这将是正确的:

(\d+)_(.+)(?<!_[ei])\.jpg

Note that the dot needs to be escaped in regular expressions.

注意点需要在正则表达式中转义。

#2


1  

it is removing the "y" because [^_e|_i] matches the y, and the .* matches everything before the y.

删除“y”是因为[^ _e | _i]匹配y,和之前的。*匹配一切y。

#3


-1  

You're forcing it to have a last character different from _e and _i. You should use this instead (note the last *):

你强迫它有一个与_e和_i不同的最后一个字符。你应该使用这个(注意最后的*):

(\d+)_(.*)[^_e|_i]*.jpg

#1


4  

You force it to chop off the last character with this part of your regex:

你强迫它用你的regex的这一部分砍掉最后一个字符:

[^_e|_i]

Which translates as: Any single character except "_", "e", "|", "i".

它的意思是:除了“_”、“e”、“|”、“i”之外的任何单个字符。

The "y" in "Lobby" matches this criterion.

“大厅”中的“y”符合这一标准。

You mean "not _e" and "not _i", obviously, but that's not the way to express it. This would be right:

你的意思显然是"not _e"和"not _i",但那不是表达的方式。这将是正确的:

(\d+)_(.+)(?<!_[ei])\.jpg

Note that the dot needs to be escaped in regular expressions.

注意点需要在正则表达式中转义。

#2


1  

it is removing the "y" because [^_e|_i] matches the y, and the .* matches everything before the y.

删除“y”是因为[^ _e | _i]匹配y,和之前的。*匹配一切y。

#3


-1  

You're forcing it to have a last character different from _e and _i. You should use this instead (note the last *):

你强迫它有一个与_e和_i不同的最后一个字符。你应该使用这个(注意最后的*):

(\d+)_(.*)[^_e|_i]*.jpg