tags:

views:

65

answers:

3

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.

Thanks!

A: 

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

(\d+)_(.*)[^_e|_i]*.jpg
Seb
Why -1? What's wrong with this??
Seb
+4  A: 

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

[^_e|_i]

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

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

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

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

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

Tomalak
That makes sense, but then how do I get a back-reference that returns the "Lobby" and not just the "y"? Thanks!
My regex does that already. Give it a try.
Tomalak
The existing \2 backref should return ‘Lobby’. (?<!...) doesn't capture any actual characters. Also, the ‘.’ in ‘.jpg’ should be a literal dot ‘\.’ and not a bare dot which means ‘any character’.
bobince
Ah, I see now. Worked like a charm, and I learned something! Thanks for the quick help.
@bobince: That's confusing. :-) You should direct your comments with the @... style. For a while I thought you meant me with your comment.
Tomalak
A: 

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

mkoryak