|
-
Aug 27th, 2007, 12:28 PM
#1
Thread Starter
Hyperactive Member
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
-
Aug 27th, 2007, 01:17 PM
#2
Frenzied Member
Re: Regular expressions, find string of all same character
Cant you use InStr or InStrRev for this ?
-
Aug 27th, 2007, 01:18 PM
#3
Addicted Member
Re: Regular expressions, find string of all same character
vb Code:
Dim i As Integer
Dim s As String
s = "xxxxxxxxxxxxxxxxxxxx"
For i = 1 to Len(s)
If Mid(s, i, 1) <> Mid(s, 1, 1) Then
MsgBox "Not all the same character"
Exit For
End If
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.

-
Aug 27th, 2007, 05:51 PM
#4
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
-
Aug 27th, 2007, 10:43 PM
#5
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
-
Dec 11th, 2007, 04:12 PM
#6
Thread Starter
Hyperactive Member
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
-
Dec 11th, 2007, 04:25 PM
#7
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).
-
Dec 11th, 2007, 07:30 PM
#8
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.
-
Dec 12th, 2007, 11:39 AM
#9
Thread Starter
Hyperactive Member
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
-
Dec 12th, 2007, 12:37 PM
#10
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
-
Jan 2nd, 2008, 01:34 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|