Regex find a string that is of certain format

Kuler Master 246 Reputation points
2022-07-20T13:54:24.36+00:00

Hello,
I have the following string/line to be found in the text made of hundred lines:

8109264F2304300BLA

81 (year) 09 (month) 26 (day) 4 (control digit) F (the value I need) 2304300 (foo value) BLA (sometimes it's not even contained in the string)

Meaning sometimes it could be 8109264F2304300.

What could be the regex that would first find this string and then fetch the letter F (it could be another letter as well) ??

Thank you so much

P.S. this is a chunk of the text that I need to search...

ARCHEEA0000000000003100<<<<<<<  
8109264F2304300BLA<<<<<<<<<<<6  
BLAH<<BLAH2<<<<<<<<<<<<<<  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-07-20T14:22:44.927+00:00

    To identify such lines, try this pattern: (?m)^\d{2}\d{1,2}\d{1,2}\d([A-Z])\d+.

    After finding the lines using Regex.Matches, use match.Groups[1] to extract the letter.

    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. LesHay 7,126 Reputation points
    2022-07-20T14:10:23.067+00:00

    Hi
    Not a regex patter/match, but perhaps you don't need the complexity of that. Here is an alternative that may be of use.
    Assuming you already have a means to isolate the line you want to check.

    		' EXAMPLE  
      
    		Dim checkline As String = "8109264F2304300BLA<<<<<<<<<<<6"  
      
    		Dim result As String = checkline.Substring(7, 1)  
      
    
    0 comments No comments

  2. Kuler Master 246 Reputation points
    2022-07-20T14:22:31.717+00:00

    Hello LesHay,

    Actually I might already found the answer. I just noticed that it's consisted of 7 digits then 1 letter and then again 7 digits so I wrote the following regex:

    var rgxGender = new Regex(@"\d{7}[A-Z]\d{7}");  
    

    It seems to be working fine. Of course once I found the match I substring the 8th character as you pointed out. Thank you

    0 comments No comments

  3. Michael Taylor 47,966 Reputation points
    2022-07-20T14:24:30.147+00:00

    A tool like Regex Editor for Visual Studio is really useful for questions like this. Here's my first pass at a RE that matches the value and captures the subset you want. Note that I'm assuming there is only 1 match per line.

       regex = new Regex(@"\d{7}(?<letter>[a-z])", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);  
         
       var match = regex.Match(input);  
       if (match.Success)  
       {  
          var letter = match.Groups["letter"]?.Value;  
       };  
    

    In your example there are actually 2 matches within your string input. My regex only covers the portion of the string you mentioned. The foo+ values you weren't specific about so I left them out. If there is a pattern after the letter then you should expand the pattern to encompass that as well.

    0 comments No comments