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
Re: Regular expressions, find string of all same character
Cant you use InStr or InStrRev for this ?
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.
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
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
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
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).
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.
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.
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
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