0

I've a TXT file and I use a Reg Expr to get the text after a string "Diagnosis Statement:"

the Full text is :

Diagnosis Statement:
6/28/2011
RZZZCG77T77G355S
Report text is here ....... end of report

I would get only the 16 digits "RZZZCG77T77G355S" into the text (16 digits mix of numbers and capital letters)

I can get the text after the string "Diagnosis Statement:" with :

(?ms)^Diagnosis Statement\s*:(?<value>.*)

and I get the code with :

^[A-Z0-9]{16}?$

But cannot get the correct way to merge both and get only the 16 digits string from text after "Diagnosis Statement:"

Can you give some help ?

1 Answers1

1

On your example, ^([A-Z0-9]{16})$ is enough. Test it here.

Do you have another place in the document where you find text with a format of 16 char, with only captials and digits ? If no, this regex is enough.

With the following regex, you can find the first thing you find with the good format, following "Diagnosis Statement:" and one line of random text.

Diagnosis Statement:\s*.*\s*\K^([A-Z0-9]{16})$

Test it here

totok
  • 1,436
  • 9
  • 28
  • Thank you very much , this get the match perfectly ! – FrancescoP8 Jul 09 '20 at 09:53
  • I removed the useless `\K` @Thefourthbird ;) – totok Jul 09 '20 at 12:48
  • Hello, I see The regex works fine in regex101.com, as are PHP expressions. When tried in C#, it throws an error saying \K is an unrecognized escape sequence. Do you know how to format it for C#? Thanks a lot – FrancescoP8 Jul 15 '20 at 13:15
  • Following [this](https://stackoverflow.com/questions/14200596/regular-expression-pattern-k-alternatives-in-c-sharp) question, the `\K` symbol is not supported. I'll search for a workaround and come back to you if I find a good one – totok Jul 15 '20 at 13:18
  • You could just use the first Regex I put in the answer for the moment – totok Jul 15 '20 at 13:19
  • Here is a workaround : `(Diagnosis Statement:\s*.*\s*)^([A-Z0-9]{16})$`. It will match the whole statement, with 2 groups. The first one is useless. Just save the second one and you're good. Test it [here](https://regex101.com/r/CnYF8r/5) – totok Jul 15 '20 at 13:21
  • Hello, thanks a lot, I see is working fine on : https://regex101.com/ but for some reason is not on: http://regexstorm.net/tester that was a suggested site to test C# , I'm checking to see why . – FrancescoP8 Jul 16 '20 at 08:51