extract strings matching a regular expression vb.net
extract strings matching a regular expression vb.net
i have a text like following
1.
2.
3.
4. Test data 1
Please identify the ID number:
# 1016108
Please check if the number above matches the number below. The comparison result
should be "True or False". You should only compare the 7 digits:
a. #1016108
Please try to compare the results from Google OCR Engine and Microsoft OCR Engine.
And choose the one that suits for this task better.
Here is a third number # 123456, please DO NOT use this number for this task
i need to extract the numbers which are followed by # alone but not the third number as there is a text "third number" infront of it. also it is mentioned that i should not take this number for matching. so i need to extract first 2 numbers(only numbers) and match and say the result .
Code from Comment
Dim mc As MatchCollection
Dim i As Int32
mc = Regex.Matches(txt, "[#]([0-9]+)")
Dim results(mc.Count - 1) As String
For i = 0 To results.Length - 1
results(i) = mc(i).Value
Next
MessageBox.Show(results.ElementAt(0).ToString)
What have you tried so far? Is there any particular error you're encountering?
– STLDeveloper
Jul 1 at 1:51
Dim mc As MatchCollection dim i as Int32 mc = Regex.Matches(txt,"[#]([0-9]+)") Dim results(mc.Count - 1) As String For i = 0 To results.Length - 1 results(i) = mc(i).Value Next system.Windows.MessageBox.Show(results.ElementAt(0).ToString)
– Seetharaman K
Jul 1 at 1:53
above code extract only the second match coz in the other matches there is a space between # and the numbers
– Seetharaman K
Jul 1 at 1:54
there are totally three matches that falls under the criteria of # followed by numbers. out of which i need to take only the first two .
– Seetharaman K
Jul 1 at 1:55
3 Answers
3
What you might do is match what you do not want and capture in a group what you do want using alternation. Your value will be in captured group 1.
bthird numbers*#s*d+b|#s*(d+)b
bthird numbers*#s*d+b|#s*(d+)b
Demo
Explanation
bthird numbers*#s*d+b
third number
b
#
s*
|
#s*(d+)b
#
d+
Or you might use a positive and a negative lookbehind to assert that what is on the left side is not third number:
(?<!bthird numbers*#s*)(?<=#s*)d+b
(?<!bthird numbers*#s*)(?<=#s*)d+b
Demo
Explanation
(?<!bthird numbers*#s*)
third number
b
#
s*
(?<=#s*)
#
d+b
Instead of matching third only, you might use S+ number instead of bthird number to match one or more times a non-whitespace character.
third
S+ number
bthird number
this gave the result perfectly. how ever, what i posted is one input. there are other 5 inputs similar to what i gave, later on i found that in all the 5 inputs third number is been asked to not to match. so what i did is i wrote a simple regex which will exact all the three numbers and match only the first two and ignore the third always!!!
– Seetharaman K
Jul 2 at 7:53
Dim mc As system.Text.RegularExpressions.MatchCollection
dim i as Int32
mc = system.Text.RegularExpressions.Regex.Matches(txt,"^#s?")
Dim results(mc.Count - 1) As String
For i = 0 To results.Length - 1
results(i) = mc(i).Value
Next
enter code here
mc(0) and mc(1) needed to be compared , MC(2) always will be left out!!
– Seetharaman K
Jul 2 at 7:54
Following your pattern and if I understand you correctly, you only want the numbers following # only if 'third number' is not written before the #
In that case this simple regex should do, check this out
Here is the regex for those who do not want to follow the link: #(.n| s.n)
This assumes that the # you want will have a new line at the end as seen from the example text you've posted.
This will also take care of the inconsistency where there can be a space after #
Hi, if you're going to downvote please state your reason for doing so, so that I can understand and improve. Thanks.
– NoobProgrammer
Jul 1 at 5:13
I ain't down-voter, but I guess that if I understand you correctly is what gave you surprise :)
– JohnyL
Jul 1 at 8:23
no, i think what you have understood about the problem is wrong . i wanted all the numbers which is followed by # but i shold not take the third number becasure it is mentioned that i should not take it !!!! but anyway the big deal here is to extract the numbers after the hash. thats the first challange
– Seetharaman K
Jul 2 at 7:46
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please explicitly tell us what the matches are. I am seeing multiple "numbers" with text in front of them.
– Tim Biegeleisen
Jul 1 at 1:48