-
[RESOLVED] Small help needed with this regex
Hi...
I want to extract mark1=90 from the string mark1=90 For English3 & English2. And I tried the following code. But it doesn't seems to be working because it is returning blank value :(
BTW, I'm not good in RegEX.
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strTemp As String
strTemp = "mark1=90 For English"
Dim aa As New System.Text.RegularExpressions.Regex("^[0-9]+$")
MessageBox.Show(aa.Match(strTemp).Value)
End Sub
Any help would be great... Thanks...:wave:
-
Re: Small help needed with this regex
Unless you are trying to learn Regex, these can easily achieved by just using the SubString Function
-
Re: Small help needed with this regex
Quote:
Originally Posted by
aashish_9601
Unless you are trying to learn Regex, these can easily achieved by just using the SubString Function
Yeah... I'm trying to learn RegEx...:wave:
-
Re: Small help needed with this regex
After some small modifications of the code based on another tutorial found on the internet:
vb Code:
Dim input As String
Dim pattern As String = ".=[0-9]+"
Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
input = "mark1=90 For English"
Dim matches As MatchCollection = rgx.Matches(input)
If matches.Count > 0 Then
For Each match As Match In matches
MsgBox(" " + match.Value)
Next
End If
This will give 1=90 as the result. But when I tried this pattern: ^.=[0-9]+, it's not showing anything :(
Why didn't it start from the beginning ? Because what I want to get is mark1=90
-
Re: Small help needed with this regex
This will give you mark1=90 as result:
VB.Net Code:
'regex with 1 capture group: (.*)
Dim srgx As New System.Text.RegularExpressions.Regex("(.*)\sFor English3 & English2")
'text you need to parse
Dim tmpstring As String = "mark1=90 For English3 & English2"
'Find Capture Group in:
Dim matchG As System.Text.RegularExpressions.MatchCollection = srgx.Matches(tmpstring)
MsgBox(matchG(0).Groups(1).ToString())
If you had multiple different entry strings, let me know to make a little change to above code, but then I will need a few more samples...
-
Re: Small help needed with this regex
Thanks Zeljko...:wave:
I'll provide some samples:
Code:
mark1=90 For English3 & English2
mark5=110, good work
mark11=2&he failed for malayalam
So,
- it is single lined
- contains the format some_characters=[numbers]some_other_characters
- i want to grab the string from the begining to =[numbers]
:wave:
-
Re: Small help needed with this regex
With this samples the new regex is:
vb Code:
Dim srgx As New System.Text.RegularExpressions.Regex("(mark\d*=\d*)")
MsgBox(matchG(0).Groups(1).ToString())
MsgBox(matchG(1).Groups(1).ToString())
MsgBox(matchG(2).Groups(1).ToString())
-
Re: Small help needed with this regex
or if you need to get something like this with 3 rule:
"i want to grab the string from the begining to =[numbers]"
VB.Net Code:
mark1=90 For English3 & English2
mark=110, good work
mar1=2&he failed for malayalam
The regex would be:
Dim srgx As New System.Text.RegularExpressions.Regex("(.*=\d*)")
-
Re: Small help needed with this regex
-
Re: Small help needed with this regex
Why not ^\D+\d+ in multiline mode :p
-
Re: Small help needed with this regex
Quote:
Originally Posted by
Zeljko
or if you need to get something like this with 3 rule:
"i want to grab the string from the begining to =[numbers]"
VB.Net Code:
mark1=90 For English3 & English2
mark=110, good work
mar1=2&he failed for malayalam
The regex would be:
Dim srgx As New System.Text.RegularExpressions.Regex("(.*=\d*)")
Yeah.. That worked...:thumb:
If don't you mind, can you give me a brief explanation on the pattern. Because I'm trying to learn. :wave:
Quote:
Originally Posted by
minitech
Why not ^\D+\d+ in multiline mode :p
That doesn't display anything :(
Quote:
Originally Posted by
Zach_VB6
It will display only 1 :(
-
Re: Small help needed with this regex
Quote:
Originally Posted by
akhileshbc
If don't you mind, can you give me a brief explanation on the pattern. Because I'm trying to learn. :wave:
Here it is:
Code:
regex: (.*=\d*)
(.*=\d*) => anything inside parentheses is inside a 'capture group' - Capture group will return data to you ($1). If we have this regex: .*=(\d*) then it will return to us only last numbers (without any char before '=' sign and without equal sign)
(.*=\d*) => Matches any character, except for line breaks if dotall (global switch 's') is false. Dot will accept any text but not new line (that is what we need here)
(.*=\d*) => Matches 0 or more of the preceding token. Asterix tell us that we can have minimum 0 or preferably more of previous and in our case previous is '.' which stands for any char.
(.*=\d*) => Matches '='. Normal string.
(.*=\d*) => Matches any digit character (0-9). But it will match only one digit char!
(.*=\d*) => Matches 0 or more of the preceding token. Asterix tell us that we can have minimum 0 or preferably more of previous and in our case previous is '\d.' which stands for one digit char - we have single or two or three digit chars so we need this.
Just to let know while you are in learning phase:
With regex always exists multiple ways of doing one thing. Some ways are shorter, some longer, some better, some bulletproof...
One way is shown above...
-
Re: [RESOLVED] Small help needed with this regex
Thanks a lot...:wave:
added rep :thumb:
I have even gone through some sites that provides information about RegEx. But most of them make it confusing and complicating rather than telling it in simple ways. :)
-
Re: [RESOLVED] Small help needed with this regex
Glad to help but no reps received :rolleyes:
-
Re: [RESOLVED] Small help needed with this regex
Quote:
Originally Posted by
Zeljko
Glad to help but no reps received :rolleyes:
Extremely sorry. I forgot to do that after marking the thread as RESOLVED. Because I was chatting with my friend at that time.:)
Please check it now...:thumb: And thanks for reminding me.
-
Re: [RESOLVED] Small help needed with this regex
Thanks :cool: reps are not necessary but they are only showing a sign of gracefulness... :thumb::thumb:
-
Re: [RESOLVED] Small help needed with this regex
My code probably doesn't display anything because it's grouped - but it will match what you're trying to capture.
-
Re: [RESOLVED] Small help needed with this regex
Quote:
Originally Posted by
minitech
My code probably doesn't display anything because it's grouped - but it will match what you're trying to capture.
Can you explain ? I'm a bit confused :confused:
-
Re: [RESOLVED] Small help needed with this regex
It doesn't have parenthesis, but the match will be correct. Check the .Value property of the Match objects returned.
-
Re: [RESOLVED] Small help needed with this regex
Quote:
Originally Posted by
minitech
It doesn't have parenthesis, but the match will be correct. Check the .Value property of the Match objects returned.
Code:
Dim srgx As New System.Text.RegularExpressions.Regex("^\D+\d+")
'text you need to parse
Dim tmpstring As String = "mark1=90 For English3 & English2"
Dim matchG As System.Text.RegularExpressions.MatchCollection = srgx.Matches(tmpstring)
MsgBox(matchG(0).Value.ToString)
Output: mark1
Code:
Dim tmpstring As String = "mark11=2&he failed for malayalam"
Output: mark11
Code:
Dim tmpstring As String = "mark5=110, good work"
Output: mark110
-
Re: [RESOLVED] Small help needed with this regex
-
Re: [RESOLVED] Small help needed with this regex
Quote:
Originally Posted by
minitech
Mini, that gives the same result as in my previous post :)
-
Re: [RESOLVED] Small help needed with this regex
I forgot the = sign again, sorry. Post edited.
-
Re: [RESOLVED] Small help needed with this regex
That worked...:thumb:
Could you please explain it like Zeljko did in post#12 ?
Thanks...:wave:
-
Re: [RESOLVED] Small help needed with this regex
^, by itself, means the beginning of a line, and since the text you need is always at the beginning of the line, I use that.
\= is an = sign, escaped because it's used in some expressions.
[^(stuff)] means to match anything except for (stuff), so I match anything except for an = sign...
\= Then, I match an = sign...
\d+ and I finally match a number. \d means one digit and + means 1 or more.