Search code examples
c#.netregexrpauipath

Regex to extract data between 3 - 8 digit number and 3 letter text(casplock text)


I wanted to extract the data between 3 - 8 digit number and 3 letter text(casplock text) using regex. Any idea would be much appreciated thank you.

Data (which is a string)

"06/29/20 B QM 02004946 TEST HOME MORTGAGE TRU

03/10/20 B BC 2440720 BK OF TEST XPN

03/07/20 B QZ 00060298 CPORT RU TRU"

OUTPUT should be : TEST HOME MORTGAGE , BK OF TEST and CPORT RU


Solution

  • Maybe this

    • Zero-width positive lookbehind
    • Zero-width positive lookahead

    Update

    var pattern = @"(?<=\d{3,8}\s).*(?=\s[A-Z]{3})";
    
    var input = @"06/29/20 B QM 02004946 MY HELLO WORLD XLS
    08/29/20 B QM 0948264 MY UI PATH LMN
    08/29/20 B QM 435 MY SAMPLE DATA LMN
    06/29/20 B QM 123 MY HELLO WORLD XLS";
    
    var results = Regex
           .Matches(input, pattern, RegexOptions.Multiline)
           .Cast<Match>()
           .Select(x => x.Value);
    
    Console.WriteLine(string.Join(", ", results));
    

    Output

    MY HELLO WORLD, MY UI PATH, MY SAMPLE DATA, MY HELLO WORLD
    

    Full Demo Here


    Original

    Pattern

    (?<=\d{3,8}\s).*(?=\s[A-Z]{3}$)
    

    Usage

    var pattern = @"(?<=\d{3,8}\s).*(?=\s[A-Z]{3}$)";
    
    var strings = new List<string>()
    {
       "06/29/20 B QM 02004946 MY HELLO WORLD XLS",
       "08/29/20 B QM 0948264 MY UI PATH  LMN",
       "08/29/20 B QM 435 MY SAMPLE DATA   LMN"
    };
    
    var results = strings.Select(x => Regex.Match(x, pattern).Value);
    
    foreach (var result in results)
       Console.WriteLine(result);
    

    Output

    MY HELLO WORLD
    MY UI PATH
    MY SAMPLE DATA
    

    Full Demo here