Regex is not working

Hello There,

I have written the regex, it was working.
Now requirement has become lil complex,
I have a text file, from this text file i want to extract one digit/numberic value.
Cases

  1. This digit is present
    2.some times not present
    3.some times goes to next line

Here is the screenshot

So this number 3 can be in the same place , or it may not be there, Or it may go to next line…
and the things marked in Yellow color boxes are always fixed lines

How shall i fix this
text.txt (587 Bytes)

Regards,
Seema

Hi @Seema_S
Want to try this Regex?

(?:6.\s*_\s)(\d)(.|\s)(?:_)(?:\s)

You need to consider I built this expression doing the following assuptions:

  • The number you are looking for, it’s always preceded by a number 6 followed by a period, one or more blank spaces, then a some under scores then any space, whitespace or new line.

  • If the number is not there, there won’t be matches.

I tested the regex here: RegExr: Learn, Build, & Test RegEx

1 Like

@GersonTun

i did it as below:
_\s(\d{1})

Thank you for your regex. But i could not understand it.
Can you help me understand it…
(?:6.\s*_ \s )(\d)(. |\s )(?:_ )(?:\s )

why that ?, *
\s - is blank space
\d- digit

Let’s break it down:

(?:6.\s*_ \s ) When a group starts with ?: it means the group it’s not going to be captured for the final result, but it has to be present there. Then the number 6, followed by a period. Then any whitespace, then any underscore and finally any whitspace.

(\d) Then one single digit.

(. |\s ) Any character or any whitespace.

(?:_ *) Other non capturing group of any underscore.

(?:\s* ) Non capturing group of any whitspace.

Hi,

FYI, another solution :

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=6\.[_\s]+)\d+(?=\D+7.  Additional amount)").Value

Regards,