Sign in to follow this  
Followers 0
Andreik

Repeating Characters

8 posts in this topic

I don't know to use very well StringRegExp and I want to know if is a better way to do that:

$TEXT = "aabdabecfdbaebcfdfaaabceeabfeabbcccd"
MsgBox(0,"",Replace($TEXT))

Func Replace($TEXT)
    Local $RESULT = $TEXT
    For $LIMIT = 4 To 2 Step -1
        $RESULT = StringRegExpReplace($RESULT,"a{" & $LIMIT & "}",$LIMIT & "a")
        $RESULT = StringRegExpReplace($RESULT,"b{" & $LIMIT & "}",$LIMIT & "b")
        $RESULT = StringRegExpReplace($RESULT,"c{" & $LIMIT & "}",$LIMIT & "c")
        $RESULT = StringRegExpReplace($RESULT,"d{" & $LIMIT & "}",$LIMIT & "d")
        $RESULT = StringRegExpReplace($RESULT,"e{" & $LIMIT & "}",$LIMIT & "e")
        $RESULT = StringRegExpReplace($RESULT,"f{" & $LIMIT & "}",$LIMIT & "f")
    Next
    Return $RESULT
EndFunc

This function replace any 2 or more recurring sequences of letters in the number of appearances and the character.

Example:

abcbdddeaff

will become

abcb3dea2f

Important: Characters set have just this letters: abcdef and the main deficiency of this function is that I need to set a trivial limit but maybe somewhere in my string sequence I will cross this limit and I will have 5 or more characters recurring. This function can be improved in any way? Thanks.

When the words fail... music speaks

Share this post


Link to post
Share on other sites



Hi,

not much but :-)

$TEXT = "aabdabecfdbaebcfdfaaabceeabfeabbcccd"
MsgBox(0, "", Replace($TEXT))

Func Replace($TEXT)
    Local $RESULT = $TEXT
    For $i = 97 To 102
        For $LIMIT = 4 To 2 Step -1
            $RESULT = StringRegExpReplace($RESULT, Chr($i) & "{" & $LIMIT & "}", $LIMIT & Chr($i))
        Next
    Next
    Return $RESULT
EndFunc   ;==>Replace

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

Share this post


Link to post
Share on other sites

This was an interesting challenge.. I've never really used backreferences inside a pattern itself, but after a few tries, the below seems to work well enough. You'll need to change the '[[:alpha:]]' bit to '[a-f]' (I made it generic for replacing any repeating character):

; <CharacterRepeatFind.au3>

Local $iLoc=1,$iCount,$aPCRE,$cChar,$sPCRE='([[:alpha:]])(\1+)',$sString='jjkklliowerjklasdddf'

While 1
    $aPCRE=StringRegExp($sString,$sPCRE,1,$iLoc)
    $iLoc=@extended
    If @error Then ExitLoop
    $iCount=StringLen($aPCRE[1])+1
    $cChar=$aPCRE[0]
    ConsoleWrite("Found "&$iCount&" occurrences of '"&$cChar&"'"&@CRLF)
    ; Replace occurrences with repeat # & Char:
    $sString=StringReplace($sString,$cChar&$aPCRE[1],$iCount&$cChar,1,2)
    ; Adjust $iLoc now to new position (we just cut off $iCount-1)
    $iLoc-=$iCount-1
WEnd
ConsoleWrite("Resulting string: "&$sString&@CRLF)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Since you only deal with a small charset but with possibly unbounded repetition factors, something like this is possible albeit probably not he most efficient in all possible case:

Local $txt = 'abccccffedbbbbbbbbbbbffdaccbefdeeaaacccccccccccccccccccccccccccccccccccccccccccccccccccccce'
Local $txtin = StringRegExpReplace($txt, '(?i)(a{2,}|b{2,}|c{2,}|d{2,}|e{2,}|f{2,})', '1$1')
Local $txtout, $txtfinal
;~ ConsoleWrite('Input      : ' & $txt & @LF)
;~ ConsoleWrite('First phase : ' & $txtin & @LF)
While 1
    $txtout = StringRegExpReplace($txtin, '(?i)(\d)([a-f])\g2', '$1+1$2')
    If $txtout = $txtin Then ExitLoop
    $txtin = $txtout
;~  ConsoleWrite('Next phase  : ' & $txtin & @LF)
WEnd
$txtexec = '"' & StringRegExpReplace($txtout, '((?:1\+)+1)', '" & $1 & "') & '"'
;~ ConsoleWrite('Final phase : ' & $txtexec & @LF)
$txtfinal = Execute($txtexec)
ConsoleWrite('Result    : ' & $txtfinal & @LF)

I've placed console outputs in order to make following the process easier. Intermediate variables are only there to optionally show progress.

It was a fun exercise to use only regexps and final execute but I strongly suspect a simpler approach can beat this one, just like Ascendant did in the meantime.

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)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

One more method for you to consider

$TEXT = "aabdabecfdbaebcfdfaaabceeabfeabbcccd"
MsgBox(0,"",Replace($TEXT))

Func Replace($TEXT)
    Local $RESULT = ""
    Local $aTmp = StringSplit($TEXT,"",2)
    If Not UBound($aTmp) Then Return ""
    $RESULT = $aTmp[0]
    For $i = 1 To UBound($aTmp) - 1
        If $aTmp[$i] <> $aTmp[$i-1] Then
          $RESULT &= $aTmp[$i]
        EndIf
    Next
    Return $RESULT
EndFunc
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Is there a reason to place a number in front of the string? I ask because it's a simple regex to replace repeating Characters with a single character.

$sStr = "aabdabecfdbaebcfdfaaabceeabfeabbcccd"
$sStr2 = StringRegExpReplace($sStr, "(\w)\1+", "$1")
MsgBox(0, "Result", $sStr & @CRLF & "becomes" & @CRLF & $sStr2)

EDIT:

You might be better off using

$sStr2 = StringRegExpReplace($sStr, ([[:alpha:]])\1+", "$1")
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

For better or for worse, here is the obligatory method that does not use regular expressions.

This example shows character "b" is not selected for repeat counting. But is easy to add to the repeating characters' string if required.

Local $txt = 'aabccccffedbbbfffdaccbefdeeaaacccccccc ccce'
Local $sAcceptChar = "acdef" ; Count only these characters (Note "b" is absent)
Local $sRetStr, $iCount

For $i = 1 To StringLen($txt)
    $iCount = 1
    While (StringMid($txt, $i, 1) = StringMid($txt, $i + 1, 1)) And _
            StringInStr($sAcceptChar, StringMid($txt, $i, 1))
        $iCount += 1
        $i += 1
    WEnd
    If $iCount = 1 Then $iCount = ""

    $sRetStr &= $iCount & StringMid($txt, $i, 1)
Next

ConsoleWrite($sRetStr & @CRLF)

Share this post


Link to post
Share on other sites

Thanks guys, for your examples.

@GEOSoft I need to know the number of occurences to can reconstruct the main string later. Thanks anyway for your examples that replace repeating characters with a single character, maybe I will need this too [it's good to know :king: ]


When the words fail... music speaks

Share this post


Link to post
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
Sign in to follow this  
Followers 0