Sign in to follow this  
Followers 0
E1M1

Could anyone help me understanding how stringRegexpReplace works?

49 posts in this topic

\S should replase string as I understood from manual. () should match string between ( and ).

Why doesnt that code replace SomeDynamicString1 with "Blahh then?"

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2, SomedynamicString3, (SomedynamicStringI#1, SomedynamicString#2, Somedynamicstring#n), SomeDynamicNumberInHEX)'

MsgBox(0,0,StringRegExpReplace($s,"SomeFixedString(\S",'(BLAHH)'))

edited

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

1. Look at pcre regular expression "escape characters". "(" has to be escaped with "\("

2. \S is to match any non white space character ( which includes that comma ), however, you have to tell it to continue to look, your example only has it looking at the first non-white space.

So maybe:

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2, SomedynamicString3, (SomedynamicStringI#1, SomedynamicString#2, Somedynamicstring#n), SomeDynamicNumberInHEX)'
MsgBox(0,0,StringRegExpReplace($s,"SomeFixedString\(\S+",'(BLAHH)'))

or

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2, SomedynamicString3, (SomedynamicStringI#1, SomedynamicString#2, Somedynamicstring#n), SomeDynamicNumberInHEX)'
MsgBox(0,0,StringRegExpReplace($s,"SomeFixedString\(S\w+",'(BLAHH)'))
Edited by SmOke_N

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

Just reading it and not testing that Expression should throw an error. You didn't escape the "(" and even if you had then you should have ended up replacing "SomeFixedString(S" with "(BLAHH)" and the rest of the string would remain intact.

Try the toolkit in my signature if you want to play with RegExp. You will find it can be a handy learning tool and there are some examples already in the library database.


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

#4 ·  Posted (edited)

thank you. How do I make this SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)

look like this?

abcd("efg","SomedynamicString3",SomeDynamicString1+SomeDynamicNumberInHEX,"SOme-Thing","Type1",$SomedynamicStringI#1,"Type2",$SomedynamicStringI#2,"Typen",$SomedynamicStringI#n)

@GEOSoft Nince tool to quick test regexps. Problem is that when I read help I understand what's written there but I donk know how to combine these for my needs.

Edit: This should switch 2 strings but it doesnt. what's wrong?

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)'

MsgBox(0,0,StringRegExpReplace($s,"SomeFixedString\(\S\w+,\S\w+",'$2. $1.'))
Edited by E1M1

edited

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Combine them? Just pretend they are Lego bricks!

I admit it needs some effort to grab the working principle, but once you get it you're fine.

Edit: hit enter prematurely!

Your transformation can probably be made in one shot, but we need more precise description of somefixedstring, some-thing, somedynamixstring in order to match them in every case.

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

true. Just edited post above. Why doesnt this code below "Edit: This should switch 2 strings but it doesnt. what's wrong?" work as expected?


edited

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

That part comes with testing and experience. Just ask SmOke_N how many times he had to repeat something to me before I got it right. Of course there was once or twice when he was wrong but S**t happens.

EDIT:

Give me a while to get some other things accomplished and I'll try to get back to you using the last examples you posted with an explaination of what is happening.

Unless of course someone beats me to it.

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

You need to grasp referencing and know the rules of your strings ( Most important ).

You look for first fix xxxx, then second fix xxxx ( eg. "xxxxx,xxxx" ) and expect the engine to know what is the first reference and what is the second.

How we make the reference to what string we are wanting to deal with, is by encapsulated by parenthesis ( eg. (xxxx)(,)(xxxx) ).

That shows 3 reference points $1 $2 $3. ( Well 3, in that example, you may not want the middle one as my example below will show )

Now what I meant by "know the rules of your strings".

You have spaces in your strings after your comma's, but you don't make a rule for it.

Examine the example below:

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)'

MsgBox(0,0,StringRegExpReplace($s,"(SomeFixedString\(\S\w+),\h*(\S\w+)",'$2. $1.'))

That part comes with testing and experience. Just ask SmOke_N how many times he had to repeat something to me before I got it right. Of course there was once or twice when he was wrong but S**t happens.

Once or twice in 20,000 attempts is hardly worth mentioning :huh2:

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

#9 ·  Posted (edited)

Whole think is:

Code that looks following

SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)

And I need to get something like that:

abcd("efg","SomedynamicString3",SomeDynamicString1+SomeDynamicNumberInHEX,"SOme-Thing","Type1",$SomedynamicStringI#1,"Type2",$SomedynamicStringI#2,"Typen",$SomedynamicStringI#n)

Typen Somedynamicstring#n means that such pattern in brackets could repeat n times(this can also be 0 times), but they are all:

1) coma separated

2) and what's between comas looks alwayslike that "Type Somedynamicstring" where type is data type and Somedynamicstring is 0-9a-Z-_

SomeDynamicString1, SomedynamicString2, SOme-Thing SomedynamicString3 this Is always the same.

Here's 1 example

abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor), 0x7D610)

and after converting it manually.

dfg("StaticStringHere","stdcall",$DynamicString1+0x7D610,"void","wchar*",$wMessage,"dword",$nColor)

SomedynamicString2 is not in converted string. (so Replacing it with "")

It's basically just switching lego bricks. but the question is that how do I handle it as bricks.

to make it short:

it looks like %s(%s, %s, %s([%s %s,]n ,%d)

n means it (%s %s,) may repeat n times.

It's like function(a,b,c,d,e) where d can hold infinate parameters given "like datatype dataitself"

Edit:

@SmOke_N After I modified your code:

$s = 'SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)'

MsgBox(0,0,StringRegExpReplace($s,"(SomeFixedString\(\S\w+),\h*(\S\w+)",'SomeFixedString\( $2 $1 '))

andfor somereason output string is longer thaninput. It repeats beginning of string for somereason. What causes this?

Edited by E1M1

edited

Share this post


Link to post
Share on other sites

OK it gets clearer thanks to the dllcall style example. Understand that the engine need to have a formal description of what you meant by "dynamicstringX", which charset is may have a.s.o. Without this information a random "dynamic string" could be a complete web page like this one!

Allow me some time to digest this grammar and see what I can come up with...


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

what does a.s.o mean?


edited

Share this post


Link to post
Share on other sites

and so on


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

#13 ·  Posted (edited)

Thanks. What I found out was that if these inner brackets dont contain args, then it's written like that (void). and in this case converted string is with out last 2*n parameters.

So the whole thing is about getting args out of func and 1 of args contains n args. 1 possible solution could be creating array where beginning is args that are always there, then next n elements are stuff from inner brackets and last element is that number from end.

This way I can easily go trough array and put string together. But I am not sure if it's easier.

Note that after replacing wchar_t * I have wchar* in converted string. What's best for this? Just numerous StringReplace() calls?

Edited by E1M1

edited

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Not only it will be easier but also the only way using AutoIt PCRE.

Let me explain why I backpedal and say it can't be done with AutoIt StringRegExpReplace implementation: to have that done in one shot, the replace part needs to deal with named captured groups since numbered groups can't be used here due to the variable number of arguments. The issue is that AutoIt doesn't support \g<name> construnct in the replace part. Doing it in Perl or with a PCRE testbed can be done but that requires external tools and that isn't perfect.

Rearrangement will have to be done in the result array of a StringRegExp match.

Here's a first try which may certainly not cover all cases)

I've used the free-spacing option (?x) to break down subpatterns into something less cryptic. I also allow spurious spaces almost everywhere (except where I forgot!).

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(  (?:  \s*(\w+\s*\*?)  \s*(\w+) \s*,?  )* \)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r)
; now rearrange parts around

Rearranging should now be easy.

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

Is it possible to get part that doesnt change thatmuch into array with stringregexp?

SomeFixedString(SomeDynamicString1, SomedynamicString2,SOme-Thing SomedynamicString3, (Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n), SomeDynamicNumberInHEX)

could be

[0] = SomeDynamicString1

[1] = SomedynamicString2

[2] = SOme-Thing

[3] = SomedynamicString3

[4] = Type1 SomedynamicStringI#1,Type2 SomedynamicString#2,Typen Somedynamicstring#n)

[5] = SomeDynamicNumberInHEX

Is there better way to create such array than string(Trim)Left/Right funcs?


edited

Share this post


Link to post
Share on other sites

Try edit above


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

#17 ·  Posted (edited)

thanks. for somereason It takes dword param but not wchar_t *wMessage

Edit:

Could anyone explain me this regexp please?

1) \s*(\w+)\s*

2) \s*(\w+)\s+(\w+)\s*

3) (?: \s*(\w+\s*\*?)

How did you reach to this string?? Now you did huge work for me but if I can't learn from it, I run into problems with next thing I try to regexp.

Edited by E1M1

edited

Share this post


Link to post
Share on other sites

Why doesnt this modification work? (trying to get whole string from innerbrackets into 1 string)

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,   \s*(\w+)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r) ; now rearrange parts around

edited

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Tried this:

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(\s*(\w+)\s*\)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r) ; now rearrange parts around

and this

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(*\s*(\w+)\s*\)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r) ; now rearrange parts around

Edit:

Tried this but doesnt work

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(*)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r) ; now rearrange parts around

Edit2:

This also doesnt work

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\([^0-9 ',()a-zA-Z\.\-_])\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r) ; now rearrange parts around

but nothing works.

Edited by E1M1

edited

Share this post


Link to post
Share on other sites

Sorry for delay, couldn't do otherwise.

Try this two-bullets gun:

Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(  ([^\)]+  ) \)\s*,  \s*0x([[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r)
; now rearrange parts around
ConsoleWrite($r[5] & @LF)
Local $p = StringRegExp($r[5], "(?ix)(?:\s*(\w+(?:\s*\*)?)  \s*(\w+) \s*,?)", 3)
_ArrayDisplay($p)

You should now be able to go from there.

I'll be back again in one hour more or less and I'll explain you the logic behind all this.


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

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