Jump to content

Weird results with brackets using SRE-Replace


Recommended Posts

Hi,

I'm writing a script to cut and learn strings with SRE-Replace. So i'm reading the document's help-file.

At first it works great but later i'm try to grouping some expression, and **wolla** some weird result came up.

Actually i'm testing with a tool from the help-file:

"GUI for testing various StringRegExp() patterns - Thanks steve8tch. Credit: w0uter"

And it works great, and the result are like i guessed. But if i'm writing some in Autoit with "StringRegExpReplace()" the result isn't the same maybe i'm writing the expression wrong or it shouldn't work like the normal SRE, whatever all this is new for me and i'm mellow now. Hmmm, so i asked in forum, and yes i'm searched for StringRegExpReplace and brackets (with/without quotes) and didn't find what i actually search (weird writing here too :))

Here is my code that a write for an example to compare expression with brackets to explain what i mean:

#include <GUIConstantsEx.au3>

$spath = "E:\Program Files\test\9999\9999M_XHr9.9.9_rel99_PL9.9.9.99_A1.2.3.45_el_HX"

$hGUI = GUICreate("Test", 350, 250)
$l1 = GUICtrlCreateLabel("", 10,  15, 300, 15)
$l2 = GUICtrlCreateLabel("", 10,  50, 300, 15)

$regExVer01 = StringRegExpReplace ( $spath, '(.*?)A\d{1,}\.', '' )
GUICtrlSetData($l2, "Version : " & $regExVer01)

$regExVer02 = StringRegExpReplace ( $spath, '.*?A\d{1,}\.', '' )
GUICtrlSetData($l1, "Version : " & $regExVer02)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

Take a little look at $regExVer01 - $regExVer02 line at the expression

'(.*?)A\d{1,}\.'

and without brackets

'.*?A\d{1,}\.'

the result are coming out same but if i'm trying it with the tool from "Thanks steve8tch. Credit: w0uter" it worked like i guess (,also if i'm cut the output with "" it should have the result A1.2.3.45_el_HX with the bracket's (.*?)A\d{1,}\.) or maybe i'm wrong or understood something wrong *heheh*

Ok let's get a chance, Thank you for looking ad helping on my script. Sorry for my bad english.

Any help is appreciate.

:)

edit: add more details

Edited by Basicz

[right]Sorry for my poor english(dictionary beside)[/right][center]Search before ask will helping the community of AutoIt.[/center][center]...seeking in the search forum and help-file's, with all the most answer's that i need. Hope for you too.[/center]

Link to comment
Share on other sites

Try

$regExVer02 = StringRegExpReplace($spath, '(?i).*_(A.*)', '$1')

Br,

UEZ

Hi UEZ,

Ah for now i'm understand what in the help-file is written i mean the REMARK section:

"Remarks

To separate back-references from actual (replaced) numbers, wrap it with half-round brackets, i.e: "${1}5"."

Mellow for a long time, but now its clear also you can give the output of the matches, hmm.. so it worked like SRE.

And yes i tested your script too and the result is what i needed. But that's the way of SRE and not SRE-Replace (coz i'm trying to cut things out to get the result not to match them so actually i can SRE to match them and set flag 1-4 to get the result). So what i actually mean is nor my expression is wrong or the function maybe (MAYBE buggy) with brackets. :)

I will add more explain here. I think i'm not to understand well :P

From the code in my first post:

$regExVer01 = StringRegExpReplace ( $spath, '(.*?)A\d{1,}\.', '' )

With this expression i should replace

"everything that is before 'A' and have 1 or more digits follow and the digit has a '.' follow"

or

E:\Program Files\test\9999\9999M_XHr9.9.9_rel99_PL9.9.9.99_

should be replaced with "" and get me the result of "A1.2.3.45_el_HX"

But instead my expect result i get "2.3.45_el_HX", so i try to understand why this is going to be like that. And after looking and trying for a while. I understand that SRER doesn't recognize the brackets and reads the expression without brackest like in "$regExVer02"

$regExVer02 = StringRegExpReplace ( $spath, '.*?A\d{1,}\.', '' )

Hmm, :D so maybe i'm wrong but would to know more about that. Hope, what i write to understand. If not then try to ask very veery thank you. ;)

Thank you, UEZ for the back-reference in SRE-R, i have learn one thing more today :)

Edited by Basicz

[right]Sorry for my poor english(dictionary beside)[/right][center]Search before ask will helping the community of AutoIt.[/center][center]...seeking in the search forum and help-file's, with all the most answer's that i need. Hope for you too.[/center]

Link to comment
Share on other sites

StringRegExpReplace obviously allows for capturing groups as they are part of the PCRE syntax but essentially behaves like "replace all matching parts with replacement part for count times if defined".

You would have easier time building your pattern from translation of plain natural language explaining what you want into PCRE syntax:

"everything that is before 'A' and have 1 or more digits follow and the digit has a '.' follow"

When you use '(.*?)A\d{1,}\.' as pattern (capturing group or not), the underlined part _is_ part of the match, so it gets replaced (by the empty string), hence it disappears from the output.

There is a simple way to express what you mean above with PCRE syntax: use a lookahead _assertion_ (which doesn't consume anything, just test that it's true).

.*(?=A\d+\.)

This means: everything which is followed by A then one or more digits and then by a dot.

OTOH you can as well use StringRegExp and use a pattern which will match only the part you're interessed in.

Full PCRE syntax

Full PCRE pattern specifications

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

StringRegExpReplace obviously allows for capturing groups as they are part of the PCRE syntax but essentially behaves like "replace all matching parts with replacement part for count times if defined".

You would have easier time building your pattern from translation of plain natural language explaining what you want into PCRE syntax:

"everything that is before 'A' and have 1 or more digits follow and the digit has a '.' follow"

When you use '(.*?)A\d{1,}\.' as pattern (capturing group or not), the underlined part _is_ part of the match, so it gets replaced (by the empty string), hence it disappears from the output.

There is a simple way to express what you mean above with PCRE syntax: use a lookahead _assertion_ (which doesn't consume anything, just test that it's true).

.*(?=A\d+\.)

This means: everything which is followed by A then one or more digits and then by a dot.

OTOH you can as well use StringRegExp and use a pattern which will match only the part you're interessed in.

Full PCRE syntax

Full PCRE pattern specifications

Hi jchd,

Aha i'm reading and make to understand what you write, and so i guess the

"replace all matching parts with replacement part for count times if defined"

it's what i'm searching it's searching all matches and this is what the result show me.

From now i understand "SRER" more, how it behave's and i get something here to learn. :P

And after i found GEOSoft's tool in his signature, i tried some PCRE pattern to declare my question by my own (+ after reading your post) making me to understand how it works. Because the option's how to match it as "SRE" that shows me that "SRER" is not like the normal array match and with your explaination i can imagine how it works now. :D

Now i'm know that my PCRE pattern is wrong too. :)

(.*?) i understand that everything that is before or behind this would be catched but it's wrong.. Anyway your PCRE pattern example works great.Ok that would get me to learn and try some more pattern for now. :)

Thank you very very much to explain about the behavior of the "SRE-R".

I very appreciate and thankful.

;)

[right]Sorry for my poor english(dictionary beside)[/right][center]Search before ask will helping the community of AutoIt.[/center][center]...seeking in the search forum and help-file's, with all the most answer's that i need. Hope for you too.[/center]

Link to comment
Share on other sites

Welcome to the addictive world of SREs!

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

Welcome to the addictive world of SREs!

:)

I got some more ways to get the result.

Some with A:

([^_]+_){4}

And some without "A" that fits it's nearly to my requirement

(.*?)((?i)_A)

Final with your pattern and a little modify

.*A(?=\d+\.)

Finally i get this with some working code :P

#include <GUIConstantsEx.au3>

$spath = "E:\Program Files\test\9999\9999M_XHr9.9.9_rel99_PL9.9.9.99_A1.2.3.45_el_HX"

Global $regExVer03[10]
$hGUI = GUICreate("Test", 350, 250)
$l1 = GUICtrlCreateLabel("", 10,  15, 300, 15)
$l2 = GUICtrlCreateLabel("", 10,  50, 300, 15)
$l3 = GUICtrlCreateLabel("", 10,  85, 300, 15)
$l4 = GUICtrlCreateLabel("", 10,  120, 300, 15)
$l5 = GUICtrlCreateLabel("", 10,  155, 300, 15)

$regExVer01 = StringRegExpReplace ( $spath, '([^_]+_){4}', '' )         ;work 90% with A
$regExVer02 = StringRegExpReplace ( $spath, '(.*?)((?i)_A)', '', 1 )        ;WORK 100%
$regExVer03 = StringRegExpReplace ( $spath, '.*A(?=\d+\.)', '' )            ;WORK 100%
;~ $regExVer0 = StringRegExpReplace ( $spath, '(.*?)A\d{1,}\.', '' )        ; DOESN"T WORK
;~ $regExVer0 = StringRegExpReplace ( $spath, '(?i).*_A\d+', '' )           ; DOESN"T WORK
$regExVer04 = StringRegExpReplace($spath, '(?i).*_A(.*)', '$1')
$regExVer05 = StringRegExp( $spath, '(?i).*_A(.*)', 1)

$regExVer01_01 = StringRegExpReplace ($regExVer01, '_.*', '')
$regExVer02_01 = StringRegExpReplace ($regExVer02, '_.*', '')
$regExVer03_01 = StringRegExpReplace ($regExVer03, '_.*', '')
$regExVer04_01 = StringRegExpReplace ($regExVer04, '_.*', '')
$regExVer05_01 = StringRegExpReplace ($regExVer05[0], '_.*', '')


GUICtrlSetData($l1, "Version1 '([^_]+_){4}' with A    : " & $regExVer01_01)
GUICtrlSetData($l2, "Version2 '(.*?)((?i)_A)' without A    : " & $regExVer02_01)
GUICtrlSetData($l3, "Version3 '.*A(?=\d+\.)' without A    : " & $regExVer03_01)
GUICtrlSetData($l4, "Version4 UEZ pattern    : " & $regExVer04_01)
GUICtrlSetData($l5, "Version5 SRE match to compare SRER from UEZ    : " & $regExVer05_01)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Super moon has some side effect to me. :)

Thank you again to UEZ, jchd and Full PCRE syntax, Full PCRE pattern specifications whoever made this

[right]Sorry for my poor english(dictionary beside)[/right][center]Search before ask will helping the community of AutoIt.[/center][center]...seeking in the search forum and help-file's, with all the most answer's that i need. Hope for you too.[/center]

Link to comment
Share on other sites

The documents behind the links are from PCRE author, Philip Hazel in person, now retired from Cambridge (I seem to remember) but still working part time to support and enhance PCRE and give it for free to the world.

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