Results 1 to 11 of 11

Thread: Regular expressions, find string of all same character

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Question Regular expressions, find string of all same character

    Any regular expression gurus out there that know how to identify if a string contains all the same character?

    e.g. "11111"
    or "xxxxxxxxxxxxxxx"
    or "a"

    without having to specify the actual character/digit sought or the number of them?

    Maybe this can't be done, but it can't hurt to ask.

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Regular expressions, find string of all same character

    Cant you use InStr or InStrRev for this ?
    IIF(Post.Rate > 0 , , )

  3. #3
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: Regular expressions, find string of all same character

    vb Code:
    1. Dim i As Integer
    2. Dim s As String
    3.  
    4. s = "xxxxxxxxxxxxxxxxxxxx"
    5.  
    6. For i = 1 to Len(s)
    7.     If Mid(s, i, 1) <> Mid(s, 1, 1) Then
    8.         MsgBox "Not all the same character"
    9.         Exit For
    10.     End If
    11. Next i

    Pretty much it will so through the whole string, comparing each character to the first one. If it doesn't match, it will let you know.
    • If you found my post to be helpful, please rate me.

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Regular expressions, find string of all same character

    Just in case every millisecond counts, this is roughly twice as fast:
    Code:
    Dim i As Long
    Dim n As Long
    Dim s As String
    
    s = "xxxxxxxxxxxxxxxxxxxx"
    n = Asc(s)
    For i = 2 To Len(s)
      If Asc(Mid$(s, i, 1)) <> n Then Exit For
    Next i
    If i > Len(s) Then
      MsgBox "All same char."
    Else
      MsgBox "Not all same char."
    End If

  5. #5
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Regular expressions, find string of all same character

    Or this:
    Code:
        Dim Str As String
        Str = "xxxxxxxxxxxxxxxxxxxx"
        
        If String(Len(Str), Left$(Str, 1)) = Str Then
            MsgBox "Same"
        Else
            MsgBox "Not the same"
        End If

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Regular expressions, find string of all same character

    No, I'm looking for a "regular expression" for this,
    so I can incorporate it in a SQL query, etc.

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Regular expressions, find string of all same character

    I don't think many database systems support regular expressions in SQL statements - but all of them support basically the same as jcis posted (all that differs is the names of the functions, which unfortunately vary by database system).

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Regular expressions, find string of all same character

    Very limited, not true regular expression. You can use LIKE but bear in mind the performance overhead... and since databases service multiple users and accesses lots of data, its best to maximize performance and avoid use of LIKE.

    If this is an Oracle database you can upload Java source (reg expression erlated API also needs to be uploaded to database), wrap in in a PL/SQL procedure call, and call procedure to perform the regular expression processing. Ask around if similar feature is available in the database you are using.
    Last edited by leinad31; Dec 11th, 2007 at 07:42 PM.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Regular expressions, find string of all same character

    We're using regular expressions quite a bit here on SQL 2000

    I'm not sure where we originally got the functions from, but
    see http://www.ecodebank.com/details/?ca...ubid=0&nid=142
    or http://www.codeproject.com/managedcpp/xpregex.asp
    for examples.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Regular expressions, find string of all same character

    I'm afraid I don't know what the RegExp would be, but here is the pure T-SQL version, which I suspect would be faster:
    Code:
    REPLICATE ( LEFT(field, 1) , LEN(field) ) = field

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Regular expressions, find string of all same character

    Si_the_geek, Thanks!
    That will work even better. And I think you're right that it will be faster.

    DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

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