Page 1 of 1

PRIM_REGX... does anyone have an example using it?

Posted: Tue Oct 03, 2023 9:37 am
by Dino
Hi Guys,

Does anyone know how to use this PRIM_REGX primitive?
Do you have an example for instance how to use the split to capture a collection of elements in a path?

https://docs.lansa.com/15/en/lansa016/C ... M_REGX.htm
https://docs.lansa.com/15/en/lansa016/C ... _Split.htm

kind regards,

Re: PRIM_REGX... does anyone have an example using it?

Posted: Thu Oct 05, 2023 8:33 pm
by dominique
Hi Dino

I didn't know this class existed. It's similar to .net split method
(https://learn.microsoft.com/en-us/dotne ... ew=net-7.0)

This example splits a semicolon delimited string to a collection. It's really useful!

regards
Dominique

Code: Select all

Mthroutine Name(Regex)
Define_Com Class(#PRIM_REGX) Name(#Regex)
Define_Com Class(#PRIM_REGX.CaptureCollection) Name(#RegexCapture) Reference(*DYNAMIC)
Define_Com Class(#PRIM_REGX.Capture) Name(#RegexCaptureItem) Reference(*DYNAMIC)

Define Field(#Pattern) Type(*STRING)
Define Field(#Input) Type(*STRING)
Define Field(#ItemCounts) Type(*INT)
Define Field(#ItemCurrent) Type(*INT)


#Pattern := "(;)"
#Input := "value1;value2;value3;value4;value5"

#Regex.split Tokens(#RegexCapture) Pattern(#Pattern) Inputstring(#Input)

#ItemCounts := #RegexCapture.ItemCount
#ItemCurrent := 0

Begin_Loop
#ItemCurrent += 1
#RegexCaptureItem <= #RegexCapture.item<#ItemCurrent>

Continue If(#RegexCaptureItem.Value *EQ ';')


Use Builtin(MESSAGE_BOX_APPEND) With_Args((("Item &1: &2").substitute( #ItemCurrent.asstring #RegexCaptureItem.Value ) + (13).aschar + (10).aschar))

If (#ItemCurrent >= #ItemCounts)
Leave
Endif
End_Loop

Use Builtin(Message_box_show)
Endroutine

Re: PRIM_REGX... does anyone have an example using it?

Posted: Thu Oct 12, 2023 12:49 am
by Dino
Thank you Dominique, I can see now where was the problem.
It works fine when used in a form, or a server routine.

I was trying to use it in a web page where that seems not be possible (PRIM_REGX doesn't contain a type called PRIM_REGX).

I will call the server module to return the values. Changed it to this:

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_SRVM)
Def_List Name(#list01) Fields(#std_strng) Type(*Working)

Srvroutine Name(RegexRoutine)
List_Map For(*Output) List(#list01)
Define_Com Class(#PRIM_REGX) Name(#Regex)
Define_Com Class(#PRIM_REGX.CaptureCollection) Name(#RegexCapture) Reference(*Dynamic)

Define Field(#Pattern) Type(*String)
Define Field(#Input) Type(*String)

#Pattern := "(;)"
#Input := "value1;value2;value3;value4;value5"

#Regex.Split Tokens(#RegexCapture) Pattern(#Pattern) InputString(#Input)

For Each(#RegexCaptureItem) In(#RegexCapture)
Continue If(#RegexCaptureItem.Value *EQ ';')
#STD_STRNG := #RegexCaptureItem.Value.AsNativeString
Add_Entry To_List(#list01)
Endfor
Endroutine
End_Com