using regular expressions

using regular expressions

Postby Antonio Linares » Thu Apr 04, 2019 4:55 pm

Code: Select all  Expand view
function Main()

   local aMatch := hb_regexAll( '[a-z0-9_]+', "function Main()",.F./*CASE*/,/*line*/,/*nMat*/,/*nGet*/,.F./*onlyName*/)
   local n

   ? Len( aMatch )

   for n := 1 to Len( aMatch )
      ? "1: ",aMatch[ n,1,1 ], " from ", aMatch[ n,1,2 ],"to ",aMatch[ n,1,3 ]
   next

return nil


https://regex101.com
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: using regular expressions

Postby AntoninoP » Fri Apr 05, 2019 7:11 am

:?:

Personally prefer Debuggex the visual view helps you to understand what regex does.
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: using regular expressions

Postby Antonio Linares » Fri Aug 21, 2020 5:35 am

A tokenizer using regex:

Code: Select all  Expand view
function Main()

   local aTokens := hb_regexAll( '[a-z][a-z]*', "this is a test" )
   local aToken
   
   for each aToken in aTokens
      ? aToken[ 1 ]
   next      

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: using regular expressions

Postby Antonio Linares » Fri Aug 21, 2020 5:41 am

tokenizer with positions

Code: Select all  Expand view
function Main()

   local aTokens := hb_regexAll( '[a-z][a-z]*', "This is a test", .F.,,,,.F. )
   local aToken

   for each aToken in aTokens
      ? aToken[ 1 ][ 1 ], "from", aToken[ 1 ][ 2 ], "to", aToken[ 1 ][ 3 ]
   next      

return nil
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: using regular expressions

Postby AntoninoP » Fri Aug 21, 2020 12:00 pm

AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: using regular expressions

Postby Antonio Linares » Fri Aug 21, 2020 12:09 pm

Antonino,

They don't work with Harbour regex. The one that I have posted is the only one that I got properly working :-)

Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: using regular expressions

Postby cnavarro » Fri Aug 21, 2020 1:06 pm

A ver si sirve ( cambiar el nombre del fichero PRG que utilizo si no existe )

Code: Select all  Expand view

//----------------------------------------------------------------------------//
// Ejemplo de uso de Expresiones Regulares
// Example of using Regular Expressions
//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//

#include "Fivewin.ch"

//----------------------------------------------------------------------------//

Function Main()

   local aSamples   := { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 100, ;
                         101, 102, 103, 104, 105, 106, 107 }
   local nOpc
   local x

   // One sample

      nOpc          := 3
      RegExSample( nOpc )      

   // All samples
   For x = 1 to Len( aSamples )
      nOpc  := aSamples[ x ]
      RegExSample( nOpc )      
   Next x



Return nil

Function RegExSample( nOpc )

   Local cRegEx  := ""
   Local aMatch
   Local oItem
   Local aItems  := {}
   Local cString := ""
   Local cWords  := ""
   Local cWords1 := ""

   Local cFile   := "..\source\classes\TFivedit.prg"

   DEFAULT nOpc  := 0
   
   cString       := "En un lugar De la Mancha" + CRLF
   cString       += "de cuyo nombre no" + CRLF
   cString       += "quiero acordarme de" + CRLF
   cString       += "  de eso no quiero acordarme de" + CRLF
   cString       += "nada de nada así que descansa mucho" + CRLF
   cString       += "   Local oObj1 := New():MyClass( n, m )" + CRLF
   cString       += "   Local oObj2 := New( ):MyClass( n, m )" + CRLF
   cString       += "cVar = (parametros)" + CRLF
   cString       += "cVar = ( parameters)" + CRLF
   cString       += "cVar = (parameters )" + CRLF
   cString       += "cVar = (parameters    )" + CRLF
   cString       += "cVar = ( parameters1 )" + CRLF
   cString       += "cVar = 8" + CRLF
   cString       += "cVar   := 4" + CRLF

   //
   
   cWords1       := "(activex|" + ;
                    "bitmap|brush|btnbmp|button|buttonbmp|buttonbar|" + ;
                    "checkbox|calex|clipboard|combobox|combometro|cursor|" + ;
                    "dbcombo|dialog|dtpicker|" + ;
                    "edit|explorerbar|" + ;
                    "flatbtn|folder|folderex|font|" + ;
                    "get|graph|group|" + ;
                    "icon|image|imagelist|imgbitmap|" + ;
                    "layout|listbox|listview|" + ;
                    "mail|metafile|meter|meterex|msgbar|msgitem|" + ;
                    "outlook|" + ;
                    "panel|pen|progress|" + ;
                    "radio|radioitem|rebar|ribbonbar|richedit|richedit5|" + ;
                    "say|saybarcode|scrollbar|scrollmsg|scrollmetro|selector|slider|" + ;
                    "splitter|statusbar|switch|" + ;
                    "tabcontrol|tbbutton|tbmenu|tbseparator|timer|" + ;
                    "tmpicker|toolbar|trackbar|treeview|" + ;
                    "windows|" + ;
                    "ximage|xbrowse)"

   Do Case
      Case nOpc = 1
         // Buscar una linea que empiece por...
         cWords  := "de"
         cRegEx  := '^' + cWords

      Case nOpc = 2
         // Buscar una linea que empiece por... ( con espacios o no delante )
         cWords  := "de"
         cRegEx  := '^\s*' + cWords

      Case nOpc = 3
         // Buscar una Palabra que empiece por...
         cString := MemoRead( cFile )
         cWords  := "new"
         cRegEx  := '\b' + cWords

      Case nOpc = 4
         // Buscar una Palabra completa
         cWords  := "de"
         cRegEx  := '\b' + cWords + '\b'

      Case nOpc = 5
         // Buscar todas las palabras
         cWords  :=  ""
         cRegEx  := '\w+'  // es lo mismo que: '[a-z0-9_]+'

      Case nOpc = 6
         // Buscar si exite :=  ó  =
         cWords  := "cvar"
         cRegEx  := '\b' + cWords + '\b.{0,20}:=' + "|" + '\b' + cWords + '\b.{0,20}='

      Case nOpc = 7
         // Buscar si exite ':='  ó  '='
         cWords  := ".*"
         cRegEx  := '\b' + cWords + '\b.{0,20}:=' + "|" + '\b' + cWords + '\b.{0,20}='


      Case nOpc = 8
         // Buscar una(s) Palabra(s) completa(s) entre los paréntesis
         cWords  := ".*"
         cRegEx  := "\(" + cWords + "\)"

      Case nOpc = 9
         //
         cWords  := ""
         cRegEx  := "New\(" + cWords + "\)"

      Case nOpc = 10
         //
         cWords  := ".*"
         cRegEx  := "New\(" + cWords + "\)"

      Case nOpc = 11
         // Busca definicion de controles con @
         cString := MemoRead( cFile )
         cWords  := "@"
         cRegEx  := '^\s*' + cWords + ".*" + "\b" + cWords1

      Case nOpc = 12
         // Busca definicion de controles con DEFINE
         cString := MemoRead( cFile )
         cWords  := "define|redefine"
         cRegEx  := '^\s*' + cWords + ".*" + "\b" + cWords1

      Case nOpc = 13
         // Busca definicion de controles con DEFINE
         cString := MemoRead( cFile )
         cWords  := "(database|editvars|fwconnect|ini|menu|menuitem|print|report)"
         cRegEx  := '^\s*' + cWords + ".*" + "\b"

      Case nOpc = 14
         // Busca definicion de controles con DEFINE
         cString := MemoRead( cFile )
         cWords  := "(include|define|xtranslate|xcommand|pragma)"
         cRegEx  := '^\s*#' + cWords + ".*" + "\b"

      // Siguiente paso, leer lineas que contengan @ como primer caracter
      // Leer lineas que tengan como primera palabra "DEFINE", ojo, que no tengan #
      // Leer lineas que tengan como primera palabra "ACTIVATE" ¿?
      // Todo eso para detectar qué clase estamos utilizando
      Case nOpc = 100
         cString := MemoRead( cFile )
         cWords  := "class"
         cRegEx  := '^\s*' + cWords + "\b" + ".*" + "\b"

      Case nOpc = 101
         cString := MemoRead( cFile )
         cWords  := "endclass"
         cRegEx  := '^\s*' + cWords

      Case nOpc = 102
         cString := MemoRead( cFile )
         cWords  := "classdata"
         cRegEx  := '^\s*' + cWords + ".*" + "\b"

      Case nOpc = 103
         cString := MemoRead( cFile )
         cWords  := "data"
         cRegEx  := '^\s*' + cWords + "\b" + ".*" + "\b"

      Case nOpc = 104
         cString := MemoRead( cFile )
         cWords  := ""
         cRegEx  := '^\s*' + "method" + ".*" + "class" + ".*"

      Case nOpc = 105
         cString := MemoRead( cFile )
         cWords  := ""
         cRegEx  := '^\s*' + "method" + ".*" + "inline" + ".*"

      Case nOpc = 106
         cString := MemoRead( cFile )
         cWords  := "(function|static function|func|procedure|proc|static procedure)"
         cRegEx  := '^\s*' + cWords + "\b" + ".*" + "\b"

      Case nOpc = 107
         cString := MemoRead( cFile )
         cWords  := "(local|private|static|public)"
         cRegEx  := '^\s*' + cWords + "\b" + ".*" + "\b"

   EndCase

   //
   if !Empty( nOpc )
      if !Hb_IsNil( HB_RegExComp( cRegEx, .F., ) )
         //
         aMatch    := hb_regexAll( cRegEx, cString, .F., .T., , , .F. )
         //
         For Each oItem IN aMatch
             //if oItem:__enumIndex == 2
             //            "Cadena"  , "Posicion Inicial", "Posicion Final"
             AAdd( aItems, { oItem[ 1, 1 ], oItem[ 1, 2 ], oItem[ 1, 3 ] } )
         Next
         XBrowse( aItems )
      else
         MsgInfo( cRegEx, "Error" )
      endif
   endif

   //

Return nil

//----------------------------------------------------------------------------//

 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: using regular expressions

Postby AntoninoP » Fri Aug 21, 2020 1:35 pm

Image really?
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: using regular expressions

Postby Carles » Sat Aug 22, 2020 6:34 am

Hi,

Very interesting...

Regards.
C.
Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
Skype -> https://join.skype.com/cnzQg3Kr1dnk
User avatar
Carles
 
Posts: 1129
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona

Re: using regular expressions

Postby Antonio Linares » Sat Aug 22, 2020 7:50 am

Antonino,

You are right. My mistake was based on the faulty mod_harbour modpro results:

https://www.modharbour.org/modharbour_samples/modpro/modpro.prg

Curiously enough, that code fails as I said when running it from modpro. I wonder why it happens like that.
When directly executed using mod_harbour it works fine, as expected.

thank you for your great feedback
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: using regular expressions

Postby Antonio Linares » Sat Aug 22, 2020 11:28 am

modpro.prg bug detected and fixed ;-)

We should never do hb_urlDecode( hb_urlDecode( cString ) ) as "+" signs will vanish away !!!
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42060
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 14 guests