1

This is in C#. I've been bugging my head but not luck so far.

So for example

123456BVC --> 123456BVC (keep the same)
123456BV --> 123456 (remove trailing letters) 
12345V -- > 12345V (keep the same)
12345 --> 12345 (keep the same)
ABC123AB --> ABC123 (remove trailing letters) 

It can start with anything.

I've tried @".*[a-zA-Z]{2}$" but no luck

This is in C# so that I always return a string removing the two trailing letters if they do exist and are not preceded with another letter.

Match result = Regex.Match(mystring, pattern);
return result.Value;
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
exevio
  • 43
  • 6

2 Answers2

0

Your @".*[a-zA-Z]{2}$" regex matches any 0+ characters other than a newline (as many as possible) and 2 ASCII letters at the end of the string. You do not check the context, so the 2 letters are matched regardless of what comes before them.

You need a regex that will match the last two letters not preceded with a letter:

(?<!\p{L})\p{L}{2}$

See this regex demo.

Details:

  • (?<!\p{L}) - fails the match if a letter (\p{L}) is found before the current position (you may use [a-zA-Z] if you only want to deal with ASCII letters)
  • \p{L}{2} - 2 letters
  • $ - end of string.

In C#, use

var result = Regex.Replace(mystring, @"(?<!\p{L})\p{L}{2}$", string.Empty);
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    A breakdown of the expression would be good, rather than just providing a spoon-fed answer to a question that is so specific it isn't likely to be of any use to anyone else – Peter Morris Jul 28 '16 at 11:18
  • 1
    No idea what you mean by a spoon fed answer, OP provided a regex OP used. I am adding explanation to 100% of my answers, see my answers in the profile. – Wiktor Stribiżew Jul 28 '16 at 11:20
  • 1
    @Wiktor Stribiżew Thank you! works as a charm. Test Cases: "132456AC", ExpectedResult = "132456") "132456ACV", ExpectedResult = "132456ACV") "ABC2D2DD", ExpectedResult = "ABC2D2") "123456", ExpectedResult = "123456") "1234AB", ExpectedResult = "1234") "1234A", ExpectedResult = "1234A") – exevio Jul 28 '16 at 12:23
  • Great, I edited the question so that it did not sound misleading. – Wiktor Stribiżew Jul 28 '16 at 12:25
0

If you're looking to remove those last two letters, you can simply do this:

string result = Regex.Replace(originalString, @"[A-Za-z]{2}$", string.Empty);

Remember that in regex $ means the end of the input or the string before a newline.

S.C.
  • 1,152
  • 8
  • 13
  • Again, this `[A-Za-z]{2}$` does not account for what comes before the 2 final letters. As for *or the string before a newline* - not in your case since you are not using the `RegexOptions.Multiline` flag. – Wiktor Stribiżew Jul 28 '16 at 11:27
  • It does not need to based on the question as I understand it. @exevio just wants the last two characters of the string removed if they are letters. No need to deal with what doesn't match since we're not replacing any of that and it doesn't change whether the last 2 are letters or not unless I completely misread something. – S.C. Jul 28 '16 at 11:29
  • See *123456BVC --> 123456BVC (keep the same)*. [Your regex](https://regex101.com/r/cZ8yK2/1) does not *keep the same*. – Wiktor Stribiżew Jul 28 '16 at 11:30
  • 1
    Ah. That contradicts the other statement of " i always returing a string be removing the two trailing letters if they do exist". Your lookback solution is clearly correct if he needs that but I'll leave this here in case it's a blanket removal. – S.C. Jul 28 '16 at 11:34