Jump to content

SRE - group position number


Recommended Posts

$n = StringRegExpReplace($s, '(TestA)(TestB)(TestC)', '$1'); <- doesn't work - just for show

I'm trying to get the "group position number" in the SRE.

ie: 1, 2 or 3

The end result would be an offset number for another piece of code i'm working on.

I can do it fairly easy with this...

Local $s = 'TestB'
Local $n = StringReplace(StringReplace(StringReplace($s, 'TestA', 1), 'TestB', 2), 'TestC', 3)
MsgBox(0, '', $n)

I thought perhaps it could be done with an SRE, but I couldn't get it to work.

Any help would be appreciated.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

So basically you won't to get the index of a string within a set of strings. This is not a problem usually solved by SRE or your (very interesting) StringReplace method.

Depending on the number of string you want to test against really dictates what you do. Typically I would approach it using an array of strings and then searching that.

In the above case, the index is in fact:

Asc(StringRight($s, 1)) - Asc('A') + 1

More information about the set of strings would allow us to help you more, patterns like that make the whole process much faster and easier.

Link to comment
Share on other sites

This was my very first attempt...

Local $n, $s = 'TestB'
Local $a = StringSplit('TestA|TestB|TestC', '|')
For $i = 1 To $a[0]
    If $a[$i] = $s Then
        $n = $i
        ExitLoop
    EndIf
Next
MsgBox(0, '', $n)

And then I was looking for alternatives.. hence the "very interesting" StringReplace.

I didn't know if one could achieve it using an SRE or not. That why I'm looking for other opinions.

@Mat - The "Strings" are as good as I've shown in the examples. It's just one word in each. There is no pattern to them. A,B,C is just to separate them for the purpose of the examples.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Ok, then a better approach (although functionally the same) as what you are doing above, is to keep a sorted array of strings, and use _ArrayBinarySearch. That's easier than manually looping and searching yourself, and also scales better (for when you have thousands of words ;) ).

That's just a slightly neater way to write exactly the same as you already have.

Local $n, $s = 'TestB'
Local $a = StringSplit('TestA|TestB|TestC', '|')
$n = _ArrayBinarySearch($a, $s, 1)
MsgBox(0, '', $n)

It will also deal with making sure the item you are looking for does actually exist (it will return -1 in that case).

Link to comment
Share on other sites

If we had access to the pcre_extra structure created by PCRE when compiling the pattern, this would be easy using (*MARK:name).

But I don't believe we have access to this structure. I don't even know if it's requested by the internals of the StringRegExp* functions.

I'll look for that anyway.

EDIT: now that I come back to think of it, there is a "simple" way to infer which capturing group fired the match but only when the capturing patterns are exclusive and forced. In the resulting array, only one element will be non-empty. So the index of the matching group is:

Local $i = 0
Do
  If $Result[$i] Then ExitLoop
Until $i = UBound($Result) - 1
$i = Mod($i + 1, UBound($Result))     ; $i is now your index or 0 if no match

I agree it isn't a one-liner, but it has the advantage to work with any regexp pattern, not only literals.

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

@Mat - It's only 3 words, thanks anyways.

@jchd - I have the idea that this would be abnormal for an SRE - judging from your reply. Thanks for looking.

@All - Thanks for your time.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Justed checked: (*MARK:mymark) is flagged as syntax error due to minimalist compile options selected for building AutoIt PCRE library.

Heck!

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 don't know if this is kosher... but this seems to work under these conditions --> strings must be known and hard coded. Any thoughts?

Local $s = 'TestB'
Local $n = UBound(StringRegExp($s, '(TestA)|(TestB)|(TestC)', 3))
MsgBox(0, '', $n)

-EDIT-

For the heck of it - some "Equivalent Statements"?

Local $n = 0, $s = 'TestB'
Switch $s
    Case 'TestA'
        $n = 1
    Case 'TestB'
        $n = 2
    Case 'TestC'
        $n = 3
Endswitch
MsgBox(0, '', $n)

Local $n = 0, $s = 'TestB'
If $s = 'TestA' Then
    $n = 1
ElseIf $s = 'TestB' Then
    $n = 2
ElseIf $s = 'TestC' Then
    $n = 3
EndIf
MsgBox(0, '', $n)
Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

You've got it. It's simpler than looking for a non-empty string in the resulting array (my post #6).

Contrary to what you say, it works with patterns as well:

Local $s = 'I take this Test with TestB'
Local $n = UBound(StringRegExp($s, '(Tw+A)|(Tw+B)|(Tw+C)', 3))
MsgBox(0, '', $n)

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

Well great!

There's always more than one way to skin a cat. Time to get the band-aids.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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