question

KulerMaster-6431 avatar image
0 Votes"
KulerMaster-6431 asked cooldadtx answered

Regex find a string that is of certain format

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<<<<<<<<<<<<<<


dotnet-csharpdotnet-visual-basicdotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered

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)


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

KulerMaster-6431 avatar image
0 Votes"
KulerMaster-6431 answered KulerMaster-6431 edited

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.