Regex to extract 2 first character from different patterns

Is this possible using a single eline regex patter ? thanks.

  1. IF contains no hyphen then get the first 2 characters
    input: FL123466
    output: FL

input: HL123466
output: HL

2.IF contains one hyphen then get first 2 characters after the hyphen.
input: A-CL123466
output: CL

input: AC-RL123466
output: RL

    1. IF contains two hyphens get the first 2 character after the first hyphen.
      input : W-AA-456789
      output: AA

input : RR-CA-456789
output: CA

what I have so far but I think this is too long

(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])

Hi,

How about the following expression?

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=-)[A-Z]{2}|^[A-Z]{2}",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value

Regards,

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.