Results 1 to 7 of 7

Thread: [RESOLVED] Find a string with REGEX?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Resolved [RESOLVED] Find a string with REGEX?

    I need to find a sequence of 8 characters in a string. The sequence is an open bracket ( followed by two upper case letters, followed by two numbers, followed by two upper case letters and finally a close bracket )

    e.g. (JN65VM)

    I could search one character at a time and piece the result together, but I wonder if this is something that regex could help with? Any code offers much appreciated!

    EDIT: Thinking about it, any six characters inside brackets/braces/parentheses should be what I need so perhaps the regex is simpler that I first thought.
    Last edited by paulg4ije; Jul 31st, 2020 at 05:53 AM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,823

    Re: Find a string with REGEX?

    see if this helps

    Code:
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim input As String = "this is a test (JN65VM) and search for (my121name) with ( dd 22 bb) brackets"
            Dim ret As New List(Of String)
    
            Dim theRegex As New Regex("\((.*?)\)")
            For Each Mt As Match In theRegex.Matches(input)
                MsgBox(Mt.Groups(0).Value) 'with brackets
                MsgBox(Mt.Groups(1).Value) 'without brackets
    
            Next
          
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: Find a string with REGEX?

    That's a step in the right direction but it does match five characters inside the brackets. Ideally I need it to only match if there are exactly six characters inside the brackets.

    EDIT: Of course I could just filter on the length of the match.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,474

    Re: Find a string with REGEX?

    EDIT: Thinking about it, any six characters inside brackets/braces/parentheses should be what I need so perhaps the regex is simpler that I first thought.
    No need for RegEx for a requirement this simple. Use the following three methods:
    1. String.StartsWith
    2. String.EndsWith
    3. String.Length

    In this case you want to check if the character length is 8 characters, the string starts with an open parenthesis, and the string ends with a closed parenthesis.

    Here is an example:
    Code:
    Private Function IsMatch(ByVal input As String) As Boolean
        Return input.Length = 8 AndAlso input.StartsWith("("c) AndAlso input.EndsWith(")"c)
    End Function
    Fiddle: Live Demo
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    New Member
    Join Date
    Aug 2017
    Posts
    7

    Re: Find a string with REGEX?

    Combine this expression with Chris's example I think you will have exactly what you want.

    I wrote this expression: \(\w{2}\d{2}\w{2}\)

    This will only match the exact form you mentioned. You can test it at https://regex101.com/ and the site will explain it to you.

    Hope that helps,
    -Kolton
    Last edited by Catch22; Jul 31st, 2020 at 08:51 AM.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: Find a string with REGEX?

    Thanks for your help guys. I'm out for a couple of hours now. Will report back later.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    682

    Re: Find a string with REGEX?

    Thanks for all the good ideas. I think something based on dday9's suggestion will work best for me.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width