Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
On Sat, 18 Jul 2009 10:54:43 -0400, "Rick Rothstein"
wrote: Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) End If Next End Function I note that given the following modification of the OP's test string: "User requested authority of emergency ID for reason NHUSER1234567 Restore of object to library LEVEL2 under remedy 61074317. 06/04/09 17:46 QPGMR" your routine returns 1234567 whereas my UDF returns "MISSING" since there are no seven digit words. (Lori's formulas return 1074317) --ron |
#2
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I am still not sure if the OP wanted the 7-digit number to stand alone (as a
"word") or not, so I just went for the first isolated 7 digits in a row... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function -- Rick (MVP - Excel) "Ron Rosenfeld" wrote in message ... On Sat, 18 Jul 2009 10:54:43 -0400, "Rick Rothstein" wrote: Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) End If Next End Function I note that given the following modification of the OP's test string: "User requested authority of emergency ID for reason NHUSER1234567 Restore of object to library LEVEL2 under remedy 61074317. 06/04/09 17:46 QPGMR" your routine returns 1234567 whereas my UDF returns "MISSING" since there are no seven digit words. (Lori's formulas return 1074317) --ron |
#3
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
My last message had the wrong opening sentence (it was meant for another
message I was working on). This is what I meant to post... The function I posted does not work as I had indicated... it finds the last 7-digit number, not the first. Here is the corrected code (plus I added the missing *MISSING* indicator)... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function -- Rick (MVP - Excel) "Rick Rothstein" wrote in message ... I am still not sure if the OP wanted the 7-digit number to stand alone (as a "word") or not, so I just went for the first isolated 7 digits in a row... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function -- Rick (MVP - Excel) "Ron Rosenfeld" wrote in message ... On Sat, 18 Jul 2009 10:54:43 -0400, "Rick Rothstein" wrote: Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) End If Next End Function I note that given the following modification of the OP's test string: "User requested authority of emergency ID for reason NHUSER1234567 Restore of object to library LEVEL2 under remedy 61074317. 06/04/09 17:46 QPGMR" your routine returns 1234567 whereas my UDF returns "MISSING" since there are no seven digit words. (Lori's formulas return 1074317) --ron |
#4
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I am still not sure if the OP wanted the 7-digit number to stand alone (as a
"word") or not, so I just went for the first isolated 7 digits in a row whether imbedded in other text or not. The reason I thought that is because of the "dot" that followed the 7-digit number in the OP's posted example text. In thinking about it, I'm guessing you took that to be a period at the end of a sentence. With that said, I made a mistake in my original function and left out the *MISSING* indicator. I just posted a corrected function against my original message for the function. -- Rick (MVP - Excel) -- Rick (MVP - Excel) "Ron Rosenfeld" wrote in message ... On Sat, 18 Jul 2009 10:54:43 -0400, "Rick Rothstein" wrote: Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) End If Next End Function I note that given the following modification of the OP's test string: "User requested authority of emergency ID for reason NHUSER1234567 Restore of object to library LEVEL2 under remedy 61074317. 06/04/09 17:46 QPGMR" your routine returns 1234567 whereas my UDF returns "MISSING" since there are no seven digit words. (Lori's formulas return 1074317) --ron |
#5
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I found this definition of a word boundary in Regular Expressions...
"A word boundary represents the spot where a letter or number meets a space, apostrophe, a period, or anything else that isn't a letter or number" Given that, this modification of my function should do what your RegExp solution does... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9]#######[!a-zA-Z0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function -- Rick (MVP - Excel) "Ron Rosenfeld" wrote in message ... On Sat, 18 Jul 2009 10:54:43 -0400, "Rick Rothstein" wrote: Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!0-9]#######[!0-9]" Then First7DigitNumber = Mid(S, X, 7) End If Next End Function I note that given the following modification of the OP's test string: "User requested authority of emergency ID for reason NHUSER1234567 Restore of object to library LEVEL2 under remedy 61074317. 06/04/09 17:46 QPGMR" your routine returns 1234567 whereas my UDF returns "MISSING" since there are no seven digit words. (Lori's formulas return 1074317) --ron |
#6
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
On Sat, 18 Jul 2009 12:33:47 -0400, "Rick Rothstein"
wrote: The reason I thought that is because of the "dot" that followed the 7-digit number in the OP's posted example text. In thinking about it, I'm guessing you took that to be a period at the end of a sentence. Yes, I did. I found this definition of a word boundary in Regular Expressions... "A word boundary represents the spot where a letter or number meets a space, apostrophe, a period, or anything else that isn't a letter or number" Not quite what I understand it to be (but close)./ The definitions I've seen indicate that a word boundary "Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters." And, at least in VBScript, a word character is a digit, letter or underscore e.g: [A-Za-z0-9_] Given that, this modification of my function should do what your RegExp solution does... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9]#######[!a-zA-Z0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function It comes pretty close. Just change this line to include the underscores: If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9_]#######[!a-zA-Z0-9_]" Then --ron |
#7
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
I had the damnedest time trying to find what RegExp considered a word
boundary (basically, each sight just kept say use \w without listing what it consider the boundary). When I finally found the one I cited, I figured it was a universal definition. Now I'm guessing there might be version differences between the various RegExp engines. Yes, I fix to account for the underbar is as you have shown it. For the archives, here is the UDF with the change you indicated... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9_]#######[!a-zA-Z0-9_]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function -- Rick (MVP - Excel) "Ron Rosenfeld" wrote in message ... On Sat, 18 Jul 2009 12:33:47 -0400, "Rick Rothstein" wrote: The reason I thought that is because of the "dot" that followed the 7-digit number in the OP's posted example text. In thinking about it, I'm guessing you took that to be a period at the end of a sentence. Yes, I did. I found this definition of a word boundary in Regular Expressions... "A word boundary represents the spot where a letter or number meets a space, apostrophe, a period, or anything else that isn't a letter or number" Not quite what I understand it to be (but close)./ The definitions I've seen indicate that a word boundary "Matches at the position between a word character (anything matched by \w) and a non-word character (anything matched by [^\w] or \W) as well as at the start and/or end of the string if the first and/or last characters in the string are word characters." And, at least in VBScript, a word character is a digit, letter or underscore e.g: [A-Za-z0-9_] Given that, this modification of my function should do what your RegExp solution does... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9]#######[!a-zA-Z0-9]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function It comes pretty close. Just change this line to include the underscores: If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9_]#######[!a-zA-Z0-9_]" Then --ron |
#8
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
On Sat, 18 Jul 2009 19:26:09 -0400, "Rick Rothstein"
wrote: I had the damnedest time trying to find what RegExp considered a word boundary (basically, each sight just kept say use \w without listing what it consider the boundary). When I finally found the one I cited, I figured it was a universal definition. Now I'm guessing there might be version differences between the various RegExp engines. Yes, I fix to account for the underbar is as you have shown it. For the archives, here is the UDF with the change you indicated... Function First7DigitNumber(S As String) As String Dim X As Long For X = 1 To Len(S) If Mid(" " & S & " ", X, 9) Like "[!a-zA-Z0-9_]#######[!a-zA-Z0-9_]" Then First7DigitNumber = Mid(S, X, 7) Exit Function End If Next First7DigitNumber = "*MISSING*" End Function And, to keep them together, here is the Regex version: =================== Option Explicit Function Seven(s As String) As String Dim re As Object, mc As Object Set re = CreateObject("vbscript.regexp") re.Pattern = "\b\d{7}\b" If re.test(s) = True Then Set mc = re.Execute(s) Seven = mc(0) Else Seven = "*MISSING*" End If End Function ==================== Here are some definitions with regard to word boundaries and some of the differences: There are three different positions that qualify as word boundaries: 1. Before the first character in the string, if the first character is a word character. 2. After the last character in the string, if the last character is a word character. 3. Between two characters in the string, where one is a word character and the other is not a word character. In all flavors, the characters [a-zA-Z0-9_] are word characters. And that is the case for VBScript. However, some of the other flavors will recognize characters from other languages, and/or unicode characters, as word characters. And I believe there is one flavor (?Python) where you can even set flags to change the definition of a word character. --ron |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Find text in free-format text field | Excel Discussion (Misc queries) | |||
Extract just numeric part of mixed text/number entry? | Excel Worksheet Functions | |||
only extract numeric value from alpha numeric cell | Excel Discussion (Misc queries) | |||
Change number (in text format) to numeric format | Excel Discussion (Misc queries) | |||
Everytime I need to format cells or text, Excel 2003 takes a lot of time or free | Excel Discussion (Misc queries) |