PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] Small help needed with this regex-VBForums
Results 1 to 25 of 25

Thread: [RESOLVED] Small help needed with this regex

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Resolved [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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim strTemp As String
    3.  
    4.         strTemp = "mark1=90 For English"
    5.  
    6.         Dim aa As New System.Text.RegularExpressions.Regex("^[0-9]+$")
    7.  
    8.         MessageBox.Show(aa.Match(strTemp).Value)
    9.  
    10.     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,...

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,135

    Re: Small help needed with this regex

    Unless you are trying to learn Regex, these can easily achieved by just using the SubString Function

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: Small help needed with this regex

    Quote Originally Posted by aashish_9601 View Post
    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,...

  4. #4

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: Small help needed with this regex

    After some small modifications of the code based on another tutorial found on the internet:
    vb Code:
    1. Dim input As String
    2.         Dim pattern As String = ".=[0-9]+"
    3.         Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase)
    4.  
    5.         input = "mark1=90 For English"
    6.  
    7.         Dim matches As MatchCollection = rgx.Matches(input)
    8.         If matches.Count > 0 Then
    9.             For Each match As Match In matches
    10.                 MsgBox("   " + match.Value)
    11.             Next
    12.         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,...

  5. #5
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Small help needed with this regex

    This will give you mark1=90 as result:
    VB.Net Code:
    1. 'regex with 1 capture group: (.*)
    2.    Dim srgx As New System.Text.RegularExpressions.Regex("(.*)\sFor English3 & English2")
    3. 'text you need to parse
    4.    Dim tmpstring As String = "mark1=90 For English3 & English2"
    5. 'Find Capture Group in:
    6.    Dim matchG As System.Text.RegularExpressions.MatchCollection = srgx.Matches(tmpstring)
    7.    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...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  6. #6

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    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,...

  7. #7
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Small help needed with this regex

    With this samples the new regex is:
    vb Code:
    1. Dim srgx As New System.Text.RegularExpressions.Regex("(mark\d*=\d*)")
    2. MsgBox(matchG(0).Groups(1).ToString())
    3. MsgBox(matchG(1).Groups(1).ToString())
    4. 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
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  8. #8
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    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:
    1. mark1=90 For English3 & English2
    2. mark=110, good work
    3. mar1=2&he failed for malayalam
    4.  
    5. The regex would be:
    6. Dim srgx As New System.Text.RegularExpressions.Regex("(.*=\d*)")
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  9. #9
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,071

    Re: Small help needed with this regex

    Why not this:
    Code:
    "^mark(\d+)=(\d+)"

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Small help needed with this regex

    Why not ^\D+\d+ in multiline mode

  11. #11

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: Small help needed with this regex

    Quote Originally Posted by Zeljko View Post
    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:
    1. mark1=90 For English3 & English2
    2. mark=110, good work
    3. mar1=2&he failed for malayalam
    4.  
    5. The regex would be:
    6. 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.

    Quote Originally Posted by minitech View Post
    Why not ^\D+\d+ in multiline mode
    That doesn't display anything

    Quote Originally Posted by Zach_VB6 View Post
    Why not this:
    Code:
    "^mark(\d+)=(\d+)"
    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,...

  12. #12
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Small help needed with this regex

    Quote Originally Posted by akhileshbc View Post
    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...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  13. #13

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    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,...

  14. #14
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Small help needed with this regex

    Glad to help but no reps received
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  15. #15

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: [RESOLVED] Small help needed with this regex

    Quote Originally Posted by Zeljko View Post
    Glad to help but no reps received
    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... And thanks for reminding me.

    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,...

  16. #16
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: [RESOLVED] Small help needed with this regex

    Thanks reps are not necessary but they are only showing a sign of gracefulness...
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.net Check my snippets: Get installed .NET versions Regex extracting Join hierarchically nested Datatables in one flattened Datatable


  17. #17
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  18. #18

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: [RESOLVED] Small help needed with this regex

    Quote Originally Posted by minitech View Post
    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,...

  19. #19
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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.

  20. #20

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: [RESOLVED] Small help needed with this regex

    Quote Originally Posted by minitech View Post
    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,...

  21. #21
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] Small help needed with this regex

    Ah, sorry:
    Code:
    ^[^\=]+\=\d+
    Last edited by minitech; Sep 11th, 2010 at 12:24 PM.

  22. #22

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    Re: [RESOLVED] Small help needed with this regex

    Quote Originally Posted by minitech View Post
    Ah, sorry:
    Code:
    ^[^\=]+\d+
    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,...

  23. #23
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: [RESOLVED] Small help needed with this regex

    I forgot the = sign again, sorry. Post edited.

  24. #24

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,647

    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,...

  25. #25
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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
  •  



Featured


Click Here to Expand Forum to Full Width