Results 1 to 21 of 21

Thread: [RESOLVED] get part of string between ( )

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Resolved [RESOLVED] get part of string between ( )

    myvar="CLI0010-ROSSI MARIO-(16)-OSPITE SINGOLO"

    in my case varfind="16"

    i need to get all between ( ).

    note:
    all part of string are variable lenght, fixed instead (xx)

  2. #2
    Fanatic Member
    Join Date
    Jan 2015
    Posts
    596

    Re: get part of string between ( )

    Code:
    Public Function GetStringBetweenTags(ByVal sSearchIn As String, ByVal sFrom As String, ByVal sUntil As String, Optional nPosAfter As Long, Optional ByVal nStartAtPos As Long = 0) As String
       ' #VBIDEUtils#***********************************************************
       ' * Programmer Name  : 
       ' * Web Site         : 
       ' * E-Mail           : 
       ' * Date             : 01/15/2001
       ' * Time             : 13:31
       ' * Module Name      : Lib_Module
       ' * Module Filename  : Lib.bas
       ' * Procedure Name   : GetStringBetweenTags
       ' * Parameters       :
       ' *                    ByVal sSearchIn As String
       ' *                    ByVal sFrom As String
       ' *                    ByVal sUntil As String
       ' *                    Optional nPosAfter As Long
       ' *                    Optional ByVal nStartAtPos As Long = 0
       ' **********************************************************************
       ' * Comments         :
       ' * This function gets in a string and two keywords
       ' * and returns the string between the keywords
       ' *
       ' **********************************************************************
    
       Dim nPos1            As Long
       Dim nPos2            As Long
       Dim nPos             As Long
       Dim nLen             As Long
       Dim sFound           As String
       Dim nLenFrom         As Long
    
    1    On Error GoTo ERROR_GetStringBetweenTags
    
    2    nLenFrom = Len(sFrom)
    
    3    nPos1 = InStr(nStartAtPos + 1, sSearchIn, sFrom, vbTextCompare)
    4    nPos2 = InStr(nPos1 + nLenFrom, sSearchIn, sUntil, vbTextCompare)
    
    5    If (nPos1 = 0) Or (nPos2 = 0) Then
    6       sFound = vbNullString
    7    Else
    8       nPos = nPos1 + nLenFrom
    9       nLen = nPos2 - nPos
    10       sFound = Mid$(sSearchIn, nPos, nLen)
    11    End If
    
    12    GetStringBetweenTags = sFound
    
    13    If nPos + nLen > 0 Then
    14       nPosAfter = (nPos + nLen) - 1
    15    End If
    
    16    Exit Function
    
    17 ERROR_GetStringBetweenTags:
    18    GetStringBetweenTags = vbNullString
    
    End Function

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: get part of string between ( )

    ONE WAY: (instead of us giving you code, you should do your own work)

    Look up Instr()...

    Use it to find the location of "(".
    Assign that location to a variable.
    Then use it to find the location of ")".
    Assign that location to a variable.
    Use the mid() function with those two variables.

    Pretty basic stuff.
    Sam I am (as well as Confused at times).

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: get part of string between ( )

    Quote Originally Posted by SamOscarBrown View Post
    ONE WAY: (instead of us giving you code, you should do your own work)

    Look up Instr()...

    Use it to find the location of "(".
    Assign that location to a variable.
    Then use it to find the location of ")".
    Assign that location to a variable.
    Use the mid() function with those two variables.

    Pretty basic stuff.
    Nice suggestion!

  5. #5
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: [RESOLVED] get part of string between ( )

    I have shown 2 approaches here -

    Code:
    Private Sub Form_Click()
     Dim sSource As String
     Dim arrWork() As String
     Dim sRightOfBracket As String
     Dim sData As String
     Dim iPos As Integer
        sSource = "CLI0010-ROSSI MARIO-(16)-OSPITE SINGOLO"
        
        ' METHOD 1    USED Split
        arrWork = Split(sSource, "(")
        sRightOfBracket = arrWork(1)
        sData = Left(sRightOfBracket, 2)
        Me.Print sData
        
        ' METHOD 2   USED InStr  and Mid
        iPos = InStr(1, sSource, "(")
        sData = Mid(sSource, iPos + 1, 2)
        Me.Print sData
    
    End Sub


    PS Sam
    Seeing as how you have been GIVEN solutions (instead of you figuring it out)
    The thread was marked RESOLVED, prior to my above post.
    So - I figured the OP had already
    figured it out
    Last edited by Bobbles; Jun 6th, 2021 at 10:24 PM.

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] get part of string between ( )

    Seeing as how you have been GIVEN solutions (instead of you figuring it out), I have one question:

    Are you sure the characters within the parentheses are always limited to just 2? Because, if not, the solution by Bobbles will not return any more than the first two characters.
    Sam I am (as well as Confused at times).

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [RESOLVED] get part of string between ( )

    Quote Originally Posted by SamOscarBrown View Post
    Seeing as how you have been GIVEN solutions (instead of you figuring it out), I have one question:

    Are you sure the characters within the parentheses are always limited to just 2? Because, if not, the solution by Bobbles will not return any more than the first two characters.
    yes, are always 2 char

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] get part of string between ( )

    Note that the code in post #5 does not look for the closing ) so it could return a false result should there be a ( somewhere in the string other than where you expect. It also will throw an error if there is no ( in the string.

    You should use Mid$() rather than Mid()

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] get part of string between ( )

    using RegEx is probably an overkill

    Code:
      With oRegex
        .Pattern = "\(((.*?))\)"
        .Global = True
        .MultiLine = True
    'etc.. more code
    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.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [RESOLVED] get part of string between ( )

    my esperiment, wath about:
    Mid(MyVar, InStr(MyVar, "(") + 1, InStr(MyVar, ")") - InStr(MyVar, "(") - 1)

  11. #11
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [RESOLVED] get part of string between ( )

    since its always 2, and theres always a (xx) the 2nd method from Bobbles seems to be the best one.

    iPos = InStr(1, sSource, "(")
    sData = Mid(sSource, iPos + 1, 2)

    or a 1 liner

    sData = Mid(sSource, InStr(sSource, "(") + 1, 2)

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] get part of string between ( )

    Avoid false hits.

    Code:
    Option Explicit
    
    Private Function Extract(ByRef Text As String) As String
        Dim L As Long
        Dim R As Long
    
        L = InStr(1, Text, "(") + 1
        If L > 1 Then
            R = InStr(L, Text, ")")
            If R > 0 Then
                Extract = Mid$(Text, L, R - L)
            End If
        End If
    End Function
    
    Private Sub Main()
        MsgBox """" & Extract("CLI0010-ROSSI MARIO-(16)-OSPITE SINGOLO") & """" _
             & vbNewLine _
             & """" & Extract("CLI0010-ROSSI MARIO-(16-OSPITE SINGOLO") & """" _
             & vbNewLine _
             & """" & Extract("(CLI0010)-ROSSI MARIO-16-OSPITE SINGOLO") & """"
    End Sub
    Last edited by dilettante; Jun 6th, 2021 at 01:32 PM.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] get part of string between ( )

    Really glad to see you wrote some of your own code, Luca! You're getting there.
    Sam I am (as well as Confused at times).

  14. #14
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: [RESOLVED] get part of string between ( )

    Sam,
    I added to my post 5, re your concern in your post 6

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: [RESOLVED] get part of string between ( )

    Quote Originally Posted by SamOscarBrown View Post
    Really glad to see you wrote some of your own code, Luca! You're getting there.
    +1 to that
    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.

  16. #16
    Fanatic Member
    Join Date
    Jan 2015
    Posts
    596

    Re: [RESOLVED] get part of string between ( )

    Why not using the code GetStringBetweenTags I posted?
    It does the trick (I use it for a lot of things)

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] get part of string between ( )

    @Thierry69---I'm sure it is good code, but I've found that the best use of this forum is to get IDEAS on how to become better at programming. It does little (not nothing, but little) to help someone learn VB6 by giving complete solutions.
    That having been said, I am guilty at times of 'stealing' others' codes (for specific applications), simply because I am not all that adept in the nuances of VB. BUT, it has always been after I have made some sort of attempt to write the code myself. Yes, at times I ask for 'direction'; but never asked for outright "will you do this for me?".

    From seeing Luca90 around for a while, I note that he, not only is not familiar with the nuances of VB, but is also not read in on the BASICS. I like to suggest to beginners to get a book, read it, do all the examples in it, and, of course, use MSDN. I have purchased several references, and have worked the examples through most of each chapter of them, simply to learn how to do things. (This not only applies to coding!!!!)

    There are some great little references out there available for free or at a small cost. My recommendation to beginners is to get one or two, and work through it/them, page by page. If I was younger, I'd have taken more classes,,,,I know there are lots of Internet sites to help people learn how to code.

    So, yes, I am sure your code is good...but, let's HELP those who ask for it.

    Samuel Oscar Brown (Yes, an alias, not anywhere near my real name).
    Sam I am (as well as Confused at times).

  18. #18
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: [RESOLVED] get part of string between ( )

    @Bobbles...mea culpa.
    Sam I am (as well as Confused at times).

  19. #19
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [RESOLVED] get part of string between ( )

    if possible coding is about making the functions for that particular project.
    I actually dont like those "all-purpose-functions", or those massive class-projects that gives u hundreds of functionality when I just need 10% of it.

    the same here, Luca need something and explain what,
    we could go: "a function that u can use in many situations" or "a function for this particular problem"

    and it seems Luca want to just make something for his needs, as shown in his post.

  20. #20
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] get part of string between ( )

    Quote Originally Posted by baka View Post
    if possible coding is about making the functions for that particular project.
    No, as far as "general rules" go... coding is (IMO) all about:
    "Avoiding to write the same functionality a second time" (DRY-principle).

    Quote Originally Posted by baka View Post
    I actually dont like those "all-purpose-functions"...
    Really?

    If I'd be Luca, then I would go with the already more generic function of dilettante
    (which is able to extract anything between (...), and not restricted to an "assumed length").

    I'd make it "even more generic for the future", so that it would work also with square-brackets [...],
    or basically any "LeftTag"..."RightTag" (now starting to resemble, what Thierry already posted in #2).
    Code:
    Function ExtractBetween(Text As String, Optional T1$ = "(", Optional T2$ = ")") As String
        Dim L As Long: L = InStr(1, Text, T1, vbTextCompare) + 1
        Dim R As Long: R = InStr(L, Text, T2, vbTextCompare)
    
        If L > 1 And R > 0 Then ExtractBetween = Trim$(Mid$(Text, L, R - L))
    End Function
    Quote Originally Posted by baka View Post
    ...or those massive class-projects that gives u hundreds of functionality when I just need 10% of it.
    Then you should probably throw out Direct2D from your Game-Projects -
    (because I highly doubt, that you use even 10% of what it offers)...

    Olaf

  21. #21
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: [RESOLVED] get part of string between ( )

    @Schmidt

    if u look at my projects, everything I have in my project is used.
    I also try to make all functions to work for just what the project require, so they are custom made.
    u can code however u want, nobody is saying that u cant, but I avoid that, for me is bad coding and thats my right to say.
    and I dont mind to redo a function. I enjoy that as that gives me opportunity to look into specific problem and learn new ways and also I feel I get an understanding of the whole project.
    if I add a big class, that I haven't used in many years, or maybe even never, it will feel alien and harder to grasp.
    if I instead need to create each part, I also feel the entire project is understood and I know all the parts and it will also be easier to analyze and find bugs if they appear.

    also, I use the typelib of direct2d, theres no functions or helper-modules at all.
    sure If I could I would make a typelib just for my needs. but I dont see any purpose of doing that since I know that when I compile the project only the parts used will be included in the executable.
    and for me the typelib is like an extension of basic language, so I get more commands to use, its not the same as adding a all-purpose function in a module.

    not sure why u need to butt in and go against my way of coding,
    go ahead and show whatever u want to help Luca, I actually don't mind and don't care, u can put a huge function, sure, someone will learn from it someone will use it.
    but maybe someone will want to use a "specific" function for that one need. and thats exactly what that person want.

    u can take your general rules and frame it and admire it in your home, but for me its just garbage, worth nothing.

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