Jump to content

Incrementing a string by 1


Recommended Posts

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 by jebus495
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

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 by Malkey
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 $2

You 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 together

The 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 with

Number("$2") + 1 the final numeric integer string, within autoIt quotes, has to be converted to number, then incremented

The resulting string will be fed to Execute in order to perform the operations in AutoIt and produce the final result.

Edited 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 here
RegExp tutorial: enough to get started
PCRE 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)

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...