Jesper Lund Stocholm
Jesper Lund Stocholm

Reputation: 2013

Regex for matching everything after second-to-last backslash

Via this post I found a way to get the last part of a string after a slash.

I need to use a slight modification of this:

I could use a regex that would match "Everything after the second-to-last backslash".

I rewrote the original to use backslash like this:

([^\\]+$)

And I made this code and test

public static string TrimTrail(this string value, string pattern)
{
    var regEx = new Regex(pattern);
    var result = regEx.Match(value);
    return result.Value;
}

[Test]
public void TestOfTrimmingTrail()
{
    //Arrange
    var stringToTest = @"0001 Lorem ipsum dolor sit\011 tortor neque\ 0111\interdum magn";
    var pattern = @"([^\\]+$)";
    //Act
    var result = stringToTest.TrimTrail(pattern);
    //Assert
    Assert.AreEqual(" 0111\\interdum magn", result);
}

But since I haven't been able to figure out the "second-to-last" requirement, it only returns

  Expected string length 19 but was 13. Strings differ at index 0.
  Expected: " 0111\\interdum magn"
  But was:  "interdum magn"
  -----------^

I tried adding a {2} before the negation char, but with no luck. Can you help me figure out the remaining part?

Thanks :-)

Upvotes: 2

Views: 5725

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626826

You can use

@"[^\\]+\\[^\\]+$"

See regex demo

The [^\\]+\\[^\\]+$ matches

  • [^\\]+ - 1 or more symbols other than \
  • \\ - a literal \
  • [^\\]+ - 1 or more symbols other than \
  • $ - end of string.

In C#, a more efficient way to match some substring at the end is using RegexOptions.RightToLeft modifier. Use it to make this regex search more efficient.

C# demo:

var line = @"0001 Lorem ipsum dolor sit\011 tortor neque\ 0111\interdum magn";
var pattern = @"[^\\]+\\[^\\]+$";
var result = Regex.Match(line, pattern, RegexOptions.RightToLeft);
if (result.Success)
    Console.WriteLine(result.Value); // => " 0111\interdum magn"

Just a comparison of the regex effeciency with and without RTL option at regexhero.net:

enter image description here

Upvotes: 7

Related Questions