Jump to content

Could anyone help me understanding how stringRegexpReplace works?


Recommended Posts

thank you. If you explain it, maybe you could also give me some exercises so I could practise it myself and maybe ask you if I cant sole something. Idea is that when I learn to use regexp I wouldn't have to create topic every time I need regexp.

edited

Link to comment
Share on other sites

  • Replies 48
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Of course you need some reference and practice.

Here we go for reference documents: the complete up-to-date PCRE reference site is here. You can download the source zip file which includes the html documentation (I find it easier to read and navigate than man pages).

You'll notice that the latest official release is 8.12 but current AutoIt uses 8.0. Differences are dark corner fixes, optimizations and few advanced features most users don't need.

A very good PCRE test bed is in GeoSoft signature. It's guaranted compatible with AutoIt PCREsince it's an AutoIt program using the very same native functions you can use yourself.

Now, dismantling the fore-mentionned baby goes like that.

First regexp to grap fixed portions and the inner string of variable parameters:

(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+)\s+(\w+)\s*,  \s*\(  ([^\)]+  ) \)\s*,  \s*0x([[:xdigit:]]+) \s*  \)
(?ix)   sets options: case insensitive (not really needed here) and free spacing (whitespaces in pattern are not part of the pattern = improve readability)
(\w+) captures the function name (strictly speaking, you don't need it but just in case it would serve a purpose)
    \w is a "word character" in PCRE parlance, that is 0-9 or a-z or A-Z or underscore
    \w+ means "one or more word character (= the full function name)
\s* means skip zero or more whitespace(s)  I've allowed for whitespace in the input syntax where they might appear.  I won't comment further \s*
\( matches the opening function parenthesis
\s*(\w+)\s*, captures the first string and skips the comma after it
\s*(\w+)\s+(\w+)\s*, captures the second and third strings and skips the comma after them
\s*\(  ([^\)]+  ) \)\s*,  matches the second opening parenthesis, captures what's inside, matches the second closing paren and the comma after it
\s*0x([[:xdigit:]]+) \s* matches the 0x prefix and captures the hex value
\) matches the final closing pren.

Second regexp where we explode (type, parameter_name) couples. Notice that I simplified the pattern here, you can change it back in the code

(?ix)  \s*  (\w+\s*\*?)  \s*  (\w+) \s*  ,?
(\w+\s*\*?) gets the type, possibly followed by a pointer notation * with or without intervening space(s)
(\w+) gets the parameter name
,? skip separating comma, if any (e.g. not there for the last parameter)

I leave it to you to handle errors and the case where the parameter list is empty.

Does this clear mud?

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

A little. But I dont get it why you made it that complicated?

#include <Array.au3>
$_ = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"

$a = StringRegExp($_,"\w+",3)
_ArrayDisplay($a)

Does the same work that your's with 2 arrays. Or Is there anything I forgot to test with my code?

edited

Link to comment
Share on other sites

I made it validate the formal structure of the required input.

Notice that when a pointer operator is used, the simple-minded version doesn't work.

Then you'd have to work around the types, since you can have them as "char* c" or "char * c" or "char *c", etc.

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

Thanks.

Diference is your's get wchar_t * but mine gets wchar_t instead. Is there any other difference?

Edit: Coming back to old topic

Local $p = StringRegExp($r[5], "(?ix)(?:\s*(\w+(?:\s*\*)?)  \s*(\w+) \s*,?)", 3)

Why does this code split void ?

for some reason I get

[0] = voi

[1] = d

Edited by E1M1

edited

Link to comment
Share on other sites

Ha! It expects a type + name and doesn't allow for void alone, so it splits there. Is that any better?

Local $p = StringRegExp($r[5], "(?ix)  \s* (?:(void) | (\w+\s*\*?)  \s*  (\w+)) \s*  ,?", 3)

You loose the pairing (type, name) so you need to handle void as the single element of the array.

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

1 more question.

your two-bullets gun works if string is

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

but doesnt work when string is

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

(29) : ==> Subscript used with non-Array variable.:
ConsoleWrite($r[5] & @LF)
ConsoleWrite($r^ ERROR

How to fix this problem?

Edit: your last code works much better.

Edited by E1M1

edited

Link to comment
Share on other sites

I need to move right now. I'll change the pattern to match a pointer type as well. Please give me one hour.

Thanks for your patience! Try this variants:

;~ Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (     void  ), 0x7D610)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\s*\*?)\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* (?:void\K | (\w+\s*\*?)  \s*  (\w+)) \s*  ,?", 3)
_ArrayDisplay($p)

Now with the \K thing, the array will have one empty string for (void) parameter list.

Tell me if that fixes all cases you might encounter.

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

It doesnt work if inner brackets are empty ( no void or anything, just ()

Edit:

What you did was changing from \s*(\w+)\s+(\w+)\s* to \s*(\w+\s*\*?)\s+(\w+)\s*

What's diference between (\w+\s*\*?) and (\w+)?

first matches any word character + any whitepace character

Dont really get it what's diference between + and *

and ? means that * may or may not appear?

Edited by E1M1

edited

Link to comment
Share on other sites

You still should find it manageable this way:

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

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

Yep, it wasn't part of the specs!

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

I came up with that:

Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\s*\*?)\s+(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \s*(\w+)\s*  \)", 1)

but why doesnt it work? every number should be possible to handle as string.

Or what's wrong with this?

Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\s*\*?)\s+(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \w+\)", 1)

doesnt \w+ mean it can be anything?

Edited by E1M1

edited

Link to comment
Share on other sites

The minus sign isn't part of \w range.

;~ Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
;~ Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (      void  ), 0x7D610)"
;~ Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (), 0x7D610)"
Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (), -123456)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\s*\*?)\s+(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \s*(-?[0-9]+|0x[[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r)
; now rearrange parts around
ConsoleWrite($r[5] & @LF)
; need to check for completely empty params list
Local $p = StringRegExp($r[5], "(?ix)  \s* (?:void\K | (\w+\s*\*?)  \s*  (\w+)) \s*  ,?", 3)
_ArrayDisplay($p)

This forces you to deal with the offest being 0x1234 or decimal 1234 or -1234, else you couldn't differentiate hex 0x1234 and decimal 1234.

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

Thanks. Nice expression.

How does it work?

-? tells that - may or may not exist?

[0-9] matches all numbers

[ and ] mans that any of these number can be there

+ means there can be many numbers in a row

| means or

Anything I understood wrong?

edited

Link to comment
Share on other sites

Right.

? as repeater = zero or one of the preceding

+ as repeater = one or more of the preceding

[] enclose a character class

| = alternation

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

What causes error with this?

something3(something2, something, var* __stdcall, (type var), -10014) - OK

something3(something2, something, var * __stdcall, (type var), -10014) - OK

something3(something2, something, var *__stdcall, (type var), -10014) - ERROR

Dont get it why it doesnt get error when * is next to var but does get when * is next to __stdcall?

Why doesnt this fox work

Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \*?\s*(\w+\s*\*?)\s+(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \s*(-?[0-9]+|0x[[:xdigit:]]+) \s*  \)", 1)

\*? Doesnt this tell how to handle *?

\* should match * and ? should tell that it may or may not be, but it didnt help me. Why?

Edit: Why this doesnt work?

Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\*?+\s*\*?)\s+(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \s*(-?[0-9]+|0x[[:xdigit:]]+) \s*  \)", 1)

I used +\*? which should be ok. I am unsure abput + signs but since you used it to separate regexp commands, I used them too.

Edited by E1M1

edited

Link to comment
Share on other sites

I again goofed with possible spacings...

;~ Local $s = "abc(DynamicString1, DynamicString2, void __stdcall, (wchar_t *wMessage, int nColor, dword param), 0x7D610)"
;~ Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (      void  ), 0x7D610)"
;~ Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (), 0x7D610)"
;~ Local $s = "abc(DynamicString1, DynamicString2, void * __stdcall, (), -123456)"
Local $s = "something3(something2, something, var *__stdcall, (type var), -10014)"
Local $r = StringRegExp($s, "(?ix)  (\w+) \s*\(  \s*(\w+)\s*,  \s*(\w+)\s*,  \s*(\w+\s*\*?)\s*(\w+)\s*,  \s*\(  ([^\)]*  ) \)\s*,  \s*(-?[0-9]+|0x[[:xdigit:]]+) \s*  \)", 1)
_ArrayDisplay($r)
; now rearrange parts around
ConsoleWrite($r[5] & @LF)
; need to check for completely empty params list
Local $p = StringRegExp($r[5], "(?ix)  \s* (?:void\K | (\w+\s*\*?)  \s*  (\w+)) \s*  ,?", 3)
_ArrayDisplay($p)

You must admit I provide you with a fine collection of errors!

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

jchd

I'm sure glad you jumped into this thread. Not only do I not have to post in it, I get to sit back and enjoy the entertainment and that should keep a smile on my face all day.

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!"

Link to comment
Share on other sites

All you did was changing one * to +. I dont get it what's the difference?

+ expects it to be one or more time and * expects it to be 0 or more times?

but when there is * then it's 1 time which should be good both * and + because help manual says it can be 0 or more times ( 1 is more than 0 , should match the criteria) and with 1 it must be 1 or more times, which should also match criteria because 1 = 1.

Could you explain that modification bit more please?

But finally It did everything I needed.My great thanks for this outstanding work.

Edited by E1M1

edited

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...