Sign in to follow this  
Followers 0
k3v

string processing, StringRegExp or not?

6 posts in this topic

My mind is numb from trying to understand StringRegExp (or StringRegExpReplace) without some guidance, at least in the context I think it may help me. I need to process characters that fall between alternate sets of double quotes in a string, regardless of how many sets, or even if they're closed sets. For example, in the string:

chr(34) & aXbcdef"ghiYjkl"mnopqZr"S123456"789T0 & chr(34)

ignore X, Z and T, but process Y and S (eg. replace "Y" with "^Y", but only if it's "inside" a closed set of double quotes that may or may not occur in the given string)

Caveat: Although I rarely if ever expect to encounter this , I tested the need for processing if there is an odd number of quotes, and it turns out that the last quote in a string will be treated as though it were the first in a closed set of two.. so any of the target characters that fall after the last in an odd number of quotes will also need to be processed... so in the string:

chr(34) & one"two"three"xyz & chr(34)

unless processed, y would be treated adversely.

Am I wasting my time with those functions? If not, can anyone provide some insight?

Thanks!

k3v

Share this post


Link to post
Share on other sites



I found it terribly hard to follow the logic you tried to explain.

It's best to post the logic. An example string (or more). The preferred output and why. And any attempts you've made.

For people like me, it's easier to understand a persons code than it is to understand their english most of the time :).


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.

Share this post


Link to post
Share on other sites

It can be done without StringRegExp ... a little complicated but possible:

#include<array.au3>
$stringC = "chr(34) & aXbcdef""ghiYjkl""mnopqZr""S123456""789T0 & chr(34)"
;$stringC = "chr(34) & one""two""three""xyz & chr(34)"

Dim $Result_String[100] ;array to store results
$i = 1                  ;array index
$occurence = 1          ;occurence
Do
    $1st = StringInStr($stringC, """", 0,$occurence)
    $occurence += 1
    $2nd = StringInStr($stringC, """", 0,$occurence)
    $Result_String[$i] = StringMid($stringC, $1st+1, $2nd-$1st-1)
    $i +=1
    $occurence += 1
Until StringInStr($stringC, """", 0,$occurence) = 0
_ArrayDisplay($Result_String)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

It can be done without StringRegExp ... a little complicated but possible:

CODE: AutoIt#include<array.au3>

$stringC = "chr(34) & aXbcdef""ghiYjkl""mnopqZr""S123456""789T0 & chr(34)"

;$stringC = "chr(34) & one""two""three""xyz & chr(34)"

Dim $Result_String[100] ;array to store results

$i = 1 ;array index

$occurence = 1 ;occurence

Do

$1st = StringInStr($stringC, """", 0,$occurence)

$occurence += 1

$2nd = StringInStr($stringC, """", 0,$occurence)

$Result_String[$i] = StringMid($stringC, $1st+1, $2nd-$1st-1)

$i +=1

$occurence += 1

Until StringInStr($stringC, """", 0,$occurence) = 0

_ArrayDisplay($Result_String)

Thanks enaiman, this resembles the direction I considered at first, (although your code is for more consice than mine would have been... nicely done!). I will probably continue this direction, using your example for guidance, if I can't become enlightened with StringRegExp(). Thanks for your response! k3v

I found it terribly hard to follow the logic you tried to explain.

It's best to post the logic. An example string (or more). The preferred output and why. And any attempts you've made.

Thank you for the tip, SmOke_N, I tried to be clear and brief, but "clearly" I failed in at least one of those goals :)

Under the circumstances it's difficult for me to send any code, for a number of reasons... but it seems to me that descrbing the trouble strings should convey the requirement.

The bottom line is, I'm writing a test application for an encryption/decryption program, and need to be able to throw every conceivable sharp rock at it I can come up with.

I'm sending command strings (and partial command strings) that may (or may not) contain characters reserved to DOS or C++, etc. Obviously I will encounter others, but since I don't have a clear list of characters that may truncate the encrypted result, I'll need a function I can easily grow as I run across them. For now, I'll use ampersand for my example. Note that for my testing, I'm also sending random nonsensical strings for input just to see what it will spit out... I'm beating this thing with a sledge hammer.

The first character I dealt with was a double quote... easy... replace every " with a \".

Another character causing trouble is "&". A backslash doesn't "escape" an ampersand, but no problem, a carat does... replace & with ^&...

BUT I can't prepend a carat to EVERY ampersand, only those that fall in the category I attempted to describe above... I'll try to recreate the example string with appropriate characters. If my string is created as follows:

$string = "x & y chr(34) 1 & 2 chr(34) A & B chr(34) 8 & 9 chr(34) ten"

then for this example, it's easier to envision the string like this:

x & y " 1 & 2 " A & B " 8 & 9 " ten

After a first round of processing, it will look like this:

x & y \" 1 & 2 \" A & B \" 8 & 9 \" ten

but I need this string to look like this:

x & y \" 1 ^& 2 \" A & B \" 8 ^& 9 \" ten

Notice that only the & between 1 and 2, and the one between 8 and 9 have been replaced with ^&, (paying heed to the positions of the embedded double quotes).

I started playing with brute-force logic, "on/off" switch variables in conjunction with StringInStr(), dynamic arrays, etc... and decided to see if I could understand StringRegExp()... it appears to be a far more elegant (and more importantly, pliable) solution. I will continue to study it, but hoped to find some assistance here. I hope I've made a little more sense this time, but I suspect I haven't.

There... now I've failed in brevity AND clarity :P

Share this post


Link to post
Share on other sites
:)

Share this post


Link to post
Share on other sites

Hi,

please just post a few examples like this:

string before = string after

and if that doesn't show off how to create the right pattern we'll ask again.

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

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