Discussions related to Visual Prolog
daveplummermd
VIP Member
Posts: 84
Joined: 18 Jul 2006 17:18

RegEx Predicates

Post by daveplummermd »

Good Evening. Are the "regex predicates" behaving as intended?
For example, the predicate:

Code: Select all

regex::search(RegExp,Where,CountFound, FoundLen)
When invoked with appropriate RegExp, it does return The starting position, BUT always returns the length of the entire Where string. I would think that it should return the starting and ending position of the qualifying expression (not the the len of the entire examined string.)

This behavior is the same for all the predicates contained in the Regex package.

alternatively, examining the example code provided in "packRegEx.htm" it states:

Code: Select all

Pattern = "([a-zA]{2}|0) \\1",             Str = "this is a line a aa aaa",             regEx::searchPos(Pattern,Str,false,3,25,Pos,Len),             console::writef("\nFound Pos = %d, len = %d.",Pos,Len),             % Found Pos = 17, len = 5.
Here, len is the length of the qualifying text, not the length of the entire string.
Can you advise??
Dave Plummer
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Re: RegEx Predicates

Post by Thomas Linder Puls »

I was not aware of the htm page (it only appears in the help (which I don't read)).

It actually belongs to an older package which is now retired, but I have tried this slightly updated test:

Code: Select all

clauses     %== search     run() :-         Pattern = @"([a-zA]{2}|0) \1",         Str = "this is a line a aa aaa",         if regEx::search(Pattern, Str, Pos, Len) then             stdio::writef("Found Pos = %d, len = %d.\n", Pos, Len)             % Found Pos = 2, len = 5.         end if,         fail. %fail to next clause.       run() :-         Pattern = @"([a-zA]{2}|0) \1",         Str = "this is a line a aa aaa",         if regEx::search(Pattern, Str, false, 3, 25, Pos, Len) then             stdio::writef("Found Pos = %d, len = %d.\n", Pos, Len)             % Found Pos = 17, len = 5.         end if,         fail. %fail to next clause.       %== replace     run() :-         Pattern = @"([a-zA]{2}|0) \1",         PatternRepl = @"\nat(not \1) will be \2(it was '\0)",         Str = "this is a line a aa aaa",         if ReplaceResult = regEx::search_and_replace(Pattern, Str, false, 0, 25, PatternRepl, Pos, Len) then             stdio::writef("Found at Pos = %d, len = %d.\n", Pos, Len),             % Found Pos = 2, len = 5.             stdio::writef("Replace with: %\n", ReplaceResult)             % Replace with:             % at(not is) will be (it was 'is is').             % --Note: \1='is'; \2='' because not defined; \0='is is'         end if.
And I get this output:
Found Pos = 2, len = 5.
Found Pos = 17, len = 5.
Found at Pos = 2, len = 5.
Replace with:
at(not is) will be (it was '
All the examples are somewhat strange especially the replace example. In time we may come up with some better examples. But in any case I do not get the behavior you describe all of them gives the expected length. Do notice that it is a length not an end-position.
Regards Thomas Linder Puls
PDC
daveplummermd
VIP Member
Posts: 84
Joined: 18 Jul 2006 17:18

Re: RegEx Predicates

Post by daveplummermd »

Thanks Thomas for the rapid reply.
There must be something in the way I'm constructing or presenting my regex expressions, they can be tricky to construct.

Nonetheless, back to the drawing board. I will post a full example if I continue to fail.

Thanks again.
By the way, I hate stupid Brazilian bots!
Dave Plummer
User avatar
Thomas Linder Puls
VIP Member
Posts: 1466
Joined: 28 Feb 2000 0:01

Re: RegEx Predicates

Post by Thomas Linder Puls »

I can suggest using a "regex tester".

There are plenty of them, e.g. regex101.com
Regards Thomas Linder Puls
PDC
daveplummermd
VIP Member
Posts: 84
Joined: 18 Jul 2006 17:18

Re: RegEx Predicates

Post by daveplummermd »

Thanks you. Very useful site.
Dave Plummer