jebus495 Posted April 14, 2010 Posted April 14, 2010 (edited) Here are some examples of the strings I will be dealing with xxx xxxx111 xxx11 xxxx-xxxx-1 I basically want to copy everything that isn't a number and increment what is a number by one, merge them back and paste it somewhere. Here is what I have tried. I have also tried 3-4 other methods that have also resulted in failure. #Include <Clipboard.au3> #Include <string.au3> #Include <array.au3> Global $i, $NEWpost $CBsplit = StringSplit(_ClipBoard_GetData($CF_TEXT), "") _ArrayDisplay($CBsplit) For $i = 1 To UBound($CBsplit) -1 If StringIsInt($CBsplit[$i]) Then _StringInsert($CBsplit[$i], $NEWpost, 0) Next MsgBox(0, $NEWpost, "") Exit I this particular case it would seem that _StringInsert is where I'm going wrong. =\ Edited April 14, 2010 by jebus495
MHz Posted April 14, 2010 Posted April 14, 2010 Not sure what you are doing with _StringInsert() though it is easier to use builtin functions first then perhaps consider STD UDFs. Just like ClipGet() could be used. Try this and see if it helps. #Include <Clipboard.au3> #Include <string.au3> #Include <array.au3> Global $i, $NEWpost $CBsplit = StringSplit(_ClipBoard_GetData($CF_TEXT), "") _ArrayDisplay($CBsplit) For $i = 1 To UBound($CBsplit) -1 ; if is an int, that use Int() +1 If StringIsInt($CBsplit[$i]) Then $CBsplit[$i] = Int($CBsplit[$i]) + 1 Next _ArrayDisplay($CBsplit) ; merge array elements into on string For $i = 1 To UBound($CBsplit) -1 $NEWpost &= $CBsplit[$i] & @CRLF Next MsgBox(0, "", $NEWpost) Exit
jebus495 Posted April 14, 2010 Author Posted April 14, 2010 (edited) The thing is that I need to turn 111 into 112 not 111 into 222. (I realize now that part wasn't clear) Also needs to be a single line. (nvm just needed to remove last @CRLF) Edited April 14, 2010 by jebus495
Malkey Posted April 14, 2010 Posted April 14, 2010 This :-Local $str[3] = ["xxx xxxx111", "xxx11", "xxxx-xxxx-1"] For $i = 0 To UBound($str) - 1 ConsoleWrite($str[$i] & @TAB & " -> " & _ Execute(StringRegExpReplace($str[$i], "([^0-9]*)(\d+)", '"\1" & (number("\2") +1)')) & @CRLF) Nextreturns this :-xxx xxxx111 -> xxx xxxx112 xxx11 -> xxx12 xxxx-xxxx-1 -> xxxx-xxxx-2
BrettF Posted April 14, 2010 Posted April 14, 2010 (edited) EDIT: Malkey beat me. Use his. Edited April 14, 2010 by BrettF Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
jebus495 Posted April 14, 2010 Author Posted April 14, 2010 OMG that is perfect. <3 Of course it had to be RegExp... >.>
jchd Posted April 14, 2010 Posted April 14, 2010 Sidenote: the regexp will fail when one or more digits are intermixed in the left (non-numeric) part. Here's how to workaround such case: Local $str[3] = ["xx3x xxxx111", "xx4x11", "xxxxxxx-5xxxx-19"] For $i = 0 To UBound($str) - 1 ConsoleWrite($str[$i] & @TAB & " -> " & _ Execute(StringRegExpReplace($str[$i], "(.*?)(\d+)$", '"$1" & Number("$2") +1')) & @CRLF) Next Anchoring the numeric part to the EOS make us certain we capture and increment the right (good) part. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Malkey Posted April 15, 2010 Posted April 15, 2010 (edited) To use this script, copy some text with the last character a digit. Move the cursor to where you want, and can paste the clipboard contents and press "F2". If the trailing character/s is/are digits then this trailing digit is incremented by 1. This script uses jchd's modification.HotKeySet("{ESC}", "Terminate") HotKeySet("{F2}", "_PasteChangedClipboard") ;Copy any of these to clipboard xxx xxxx111 , xxx11 , xxxx-xxxx-1 , xx4x11 Global $sOldContents While 1 Sleep(10) WEnd Func _PasteChangedClipboard() Local $sCbContents = ClipGet() If $sOldContents <> $sCbContents And StringRegExp($sCbContents, "(.*?)(\d)$") Then $sCbContents = StringReplace($sCbContents, "'", "#SingleQts#") $sCbContents = StringReplace($sCbContents, '"', "#DbleQts#") $sCbContents = Execute(StringRegExpReplace($sCbContents, "(.*?)(\d+)$", '"$1" & Number("$2") +1')) If @error Then Return $sCbContents = StringReplace($sCbContents, "#SingleQts#", "'") $sCbContents = StringReplace($sCbContents, "#DbleQts#", '"') $sOldContents = $sCbContents EndIf Send(StringRegExpReplace($sCbContents, "(\+|=|!|#|\^|\{|\})", "{\1}")) EndFunc ;==>_PasteChangedClipboard Func Terminate() Exit 0 EndFunc ;==>Terminate Edited April 15, 2010 by Malkey
BrettF Posted April 15, 2010 Posted April 15, 2010 Sidenote: the regexp will fail when one or more digits are intermixed in the left (non-numeric) part. Here's how to workaround such case: Local $str[3] = ["xx3x xxxx111", "xx4x11", "xxxxxxx-5xxxx-19"] For $i = 0 To UBound($str) - 1 ConsoleWrite($str[$i] & @TAB & " -> " & _ Execute(StringRegExpReplace($str[$i], "(.*?)(\d+)$", '"$1" & Number("$2") +1')) & @CRLF) Next Anchoring the numeric part to the EOS make us certain we capture and increment the right (good) part. I am interested to how this works. Could you maybe explain to me what you've done? Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
jchd Posted April 15, 2010 Posted April 15, 2010 (edited) Of course Brett, here we are. I'll break down the guy like in this other post.Execute(StringRegExpReplace($str[$i], "(.*?)(\d+){:content:}quot;, '"$1" & Number("$2") +1'))Regexp pattern part is (.*?)(\d+)$(.*?) we capture with () the shortest string ? composed of zero or more times * any character . that will make the whole regexp match. The capture will be $1 in replace pattern(\d+)$ then we capture with () the longest string of one or more + decimal digits \d which we require to end at end of input string $. This will be capture $2You imagine that there can be some backtracking when several repetitions in the pattern might split the matches at different places. But with anchoring the final pattern to the end of input, the engine has something to align to and the optimizing compiler will surely start the matching from there in the absence of any other litteral required match.We need a non-greedy first part, since (.*)(\d+) could well split "abc123" into "abc12" and "3" without violating any rule, which is not what we want: "abc39" split into abc3 and 9 would give abc310 instead of abc40.Anchoring by $ also has the benefit that in xxx5xxx3 the second part will be "3" and not the first match we would get, "5", if we din't anchor.For the replacement, we have to find a way to increment the number then concatenate both parts back togetherThe pattern "$1" & Number("$2") +1 will produce"$1" which is the left non numeric part inside quotes (hence a valid AutoIt string) & which we concatenate withNumber("$2") + 1 the final numeric integer string, within autoIt quotes, has to be converted to number, then incrementedThe resulting string will be fed to Execute in order to perform the operations in AutoIt and produce the final result. Edited April 15, 2010 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
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