sensalim Posted March 5, 2008 Share Posted March 5, 2008 I have a problem. Say I have these strings: "Cityname ST 11111" "La City ST 22222-3333" "City Of City, ST 33333" Notice they are city-state-zip in one string. Notice also that city can be one or more words, followed by an optional comma, 2-letter word for states then finally a space and zip. I need a function smart enough to separate any string like those 3 into 3 variables (city, state and zip). I can detect zip fairly easy, but I don't know how to do the other two. Can anyone help? Thanks! Link to comment Share on other sites More sharing options...
Monamo Posted March 5, 2008 Share Posted March 5, 2008 I have a problem. Say I have these strings: "Cityname ST 11111" "La City ST 22222-3333" "City Of City, ST 33333" Notice they are city-state-zip in one string. Notice also that city can be one or more words, followed by an optional comma, 2-letter word for states then finally a space and zip. I need a function smart enough to separate any string like those 3 into 3 variables (city, state and zip). I can detect zip fairly easy, but I don't know how to do the other two. Can anyone help? Thanks!Those with the RegEx knowledge could likely provide something shorter and cleaner, but this should work for you: CODE ;$entry = "Cityname ST 11111" ;$entry = "La City ST 22222-3333" $entry = "City Of City, ST 33333" $aSplit = StringSplit($entry, " ") For $i = 1 To $aSplit[0] If StringRight($aSplit[$i], 1) = "," Then $aSplit[$i] = StringTrimRight($aSplit[$i], 1) EndIf Next $zip = $aSplit[$aSplit[0]] $state = $aSplit[$aSplit[0] - 1] $city = "" ConsoleWrite($city &@CRLF) If $aSplit[0] > 3 Then For $i = 1 To ($aSplit[0] - 2) $city = $city &$aSplit[$i] & " " ConsoleWrite($city &@CRLF) Next Else $city = $aSplit[1] EndIf StringStripWS($city,3) MsgBox(64, "Parsed data", $city & @CRLF & $state & @CRLF & $zip) - MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup] Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 5, 2008 Share Posted March 5, 2008 $entry1 = "Cityname ST 11111" $entry2 = "La City ST 22222-3333" $entry3 = "City Of City, ST 33333" _getEntries($entry1) _getEntries($entry2) _getEntries($entry3) Func _getEntries($text) Local $city = StringRegExp($text, '[ \w]+(?=[,]* [A-Z]{2} )', 3) Local $ST = StringRegExp($text, '[A-Z]{2}(?= \d*)', 3) Local $zip = StringRegExp($text, '[\d-]*\z', 3) ConsoleWrite(@CRLF & $city[0] & @CRLF & $ST[0] & @CRLF &$zip[0] & @CRLF ) EndFunc ;==>_getEntries Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
sensalim Posted March 5, 2008 Author Share Posted March 5, 2008 $entry1 = "Cityname ST 11111" $entry2 = "La City ST 22222-3333" $entry3 = "City Of City, ST 33333" _getEntries($entry1) _getEntries($entry2) _getEntries($entry3) Func _getEntries($text) Local $city = StringRegExp($text, '[ \w]+(?=[,]* [A-Z]{2} )', 3) Local $ST = StringRegExp($text, '[A-Z]{2}(?= \d*)', 3) Local $zip = StringRegExp($text, '[\d-]*\z', 3) ConsoleWrite(@CRLF & $city[0] & @CRLF & $ST[0] & @CRLF &$zip[0] & @CRLF ) EndFunc ;==>_getEntries DAaaaaaaang. My head exploded. Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 5, 2008 Share Posted March 5, 2008 DAaaaaaaang. My head exploded. why that? You can change the return value of the function to whatever you want. consolewrite is just for testing. I dunno how you need it get returned. I can explain the pattern I you need to know.Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Monamo Posted March 5, 2008 Share Posted March 5, 2008 DAaaaaaaang. My head exploded.lol, yep - told ya there would be a cleaner way of doing it with RegEx. I've yet to sit myself down and start learning the workings of it, but it's well worth knowing. - MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup] Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 5, 2008 Moderators Share Posted March 5, 2008 (edited) $entry1 = "Cityname ST 11111" $entry2 = "La City ST 22222-3333" $entry3 = "City Of City, ST 33333" _getEntries($entry1) _getEntries($entry2) _getEntries($entry3) Func _getEntries($text) Local $city = StringRegExp($text, '[ \w]+(?=[,]* [A-Z]{2} )', 3) Local $ST = StringRegExp($text, '[A-Z]{2}(?= \d*)', 3) Local $zip = StringRegExp($text, '[\d-]*\z', 3) ConsoleWrite(@CRLF & $city[0] & @CRLF & $ST[0] & @CRLF &$zip[0] & @CRLF ) EndFunc ;==>_getEntriesoÝ÷ Ûú®¢×¢é]ë-vÂ+a¢wz1¥«¢+Ù1½°ÀÌØí¹ÑÉ¥ÍlÑtôlÅÕ½ÐìÅÕ½Ðì°ÅÕ½Ðí ¥Ñå¹µMPÄÄÄÄÄÅÕ½Ðì°ÅÕ½Ðí1 ¥ÑäMPÈÈÈÈÈ´ÌÌÌÌÅÕ½Ðì°ÅÕ½Ðí ¥Ñä= ¥Ñä°MPÌÌÌÌÌÅÕ½Ðít°ÀÌØí ¥ÑåMÑÑi¥À)½ÈÀÌØí¤ôÄQ¼Ì($ÀÌØí ¥ÑåMÑÑi¥ÀôMÑÉ¥¹IáÀ ÀÌØí¹ÑÉ¥ÍlÀÌØí¥t°ÅÕ½Ðì ¸¬ü¤°¨ÀäÈíÌ ÀäÈíÜÀäÈíܤÀäÈíÌ ÀäÈí¬´¨ÀäÈí¨¤ÀÌØìÅÕ½Ðì°Ä¤(%}ÉÉå¥ÍÁ±ä ÀÌØí ¥ÑåMÑÑi¥À°ÀÌØí¹ÑÉ¥ÍlÀÌØí¥t¤)9á Edited March 5, 2008 by SmOke_N Fixed Exp Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
sensalim Posted March 5, 2008 Author Share Posted March 5, 2008 (edited) Ok let's learn. [ \w]+(?=[,]* [A-Z]{2} ) I may need to print the handbook but what does (?= ................ ) do? Edited March 5, 2008 by sensalim Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 5, 2008 Moderators Share Posted March 5, 2008 Ok let's learn. [ \w]+(?=[,]* [A-Z]{2} ) I may need to print the handbook but what does (?= ................ ) do?http://perldoc.perl.org/perlre.html#Regular-Expressions Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Xenobiologist Posted March 5, 2008 Share Posted March 5, 2008 Could just do it with one RegExp... Local $aEntries[4] = ["","Cityname ST 11111", "La City ST 22222-3333", "City Of City, ST 33333"], $aCityStateZip For $i = 1 To 3 $aCityStateZip = StringRegExp($aEntries[$i], "(.+?),*\s(\w\w)\s(\d+-*\d*){:content:}quot;, 1) _ArrayDisplay($aCityStateZip, $aEntries[$i]) Next Hi, nice one. But I didn't whether that was wanted. Like the grouping. Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now