|
-
Sep 5th, 2010, 02:31 AM
#1
[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...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 5th, 2010, 03:39 AM
#2
Frenzied Member
Re: Small help needed with this regex
Unless you are trying to learn Regex, these can easily achieved by just using the SubString Function
-
Sep 5th, 2010, 04:33 AM
#3
Re: Small help needed with this regex
 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...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 5th, 2010, 07:07 AM
#4
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 5th, 2010, 11:23 AM
#5
Hyperactive Member
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...
-
Sep 5th, 2010, 11:50 AM
#6
Re: Small help needed with this regex
Thanks Zeljko...
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]
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 5th, 2010, 12:07 PM
#7
Hyperactive Member
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())
Last edited by Zeljko; Sep 5th, 2010 at 12:10 PM.
Reason: Edit spell-error MatchG is raised by 1 and not Groups
-
Sep 5th, 2010, 12:39 PM
#8
Hyperactive Member
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*)")
-
Sep 5th, 2010, 02:11 PM
#9
Frenzied Member
Re: Small help needed with this regex
-
Sep 5th, 2010, 04:51 PM
#10
Re: Small help needed with this regex
Why not ^\D+\d+ in multiline mode
-
Sep 5th, 2010, 09:13 PM
#11
Re: Small help needed with this regex
 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...
If don't you mind, can you give me a brief explanation on the pattern. Because I'm trying to learn. 
 Originally Posted by minitech
Why not ^\D+\d+ in multiline mode 
That doesn't display anything 
 Originally Posted by Zach_VB6
It will display only 1
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 6th, 2010, 01:03 AM
#12
Hyperactive Member
Re: Small help needed with this regex
 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. 
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...
-
Sep 6th, 2010, 08:24 AM
#13
Re: [RESOLVED] Small help needed with this regex
Thanks a lot...
added rep 
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.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 6th, 2010, 09:19 AM
#14
Hyperactive Member
Re: [RESOLVED] Small help needed with this regex
Glad to help but no reps received
-
Sep 6th, 2010, 09:22 AM
#15
Re: [RESOLVED] Small help needed with this regex
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 6th, 2010, 09:26 AM
#16
Hyperactive Member
-
Sep 7th, 2010, 06:33 PM
#17
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.
-
Sep 7th, 2010, 09:19 PM
#18
Re: [RESOLVED] Small help needed with this regex
 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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 8th, 2010, 05:18 PM
#19
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.
-
Sep 8th, 2010, 09:25 PM
#20
Re: [RESOLVED] Small help needed with this regex
 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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 9th, 2010, 03:33 PM
#21
Re: [RESOLVED] Small help needed with this regex
Last edited by minitech; Sep 11th, 2010 at 12:24 PM.
-
Sep 9th, 2010, 10:39 PM
#22
Re: [RESOLVED] Small help needed with this regex
 Originally Posted by minitech
Mini, that gives the same result as in my previous post
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 11th, 2010, 12:24 PM
#23
Re: [RESOLVED] Small help needed with this regex
I forgot the = sign again, sorry. Post edited.
-
Sep 11th, 2010, 12:46 PM
#24
Re: [RESOLVED] Small help needed with this regex
That worked...
Could you please explain it like Zeljko did in post#12 ?
Thanks...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Sep 11th, 2010, 02:26 PM
#25
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|