Page 1 of 1

using regular expressions

Posted: Thu Apr 04, 2019 4:55 pm
by Antonio Linares

Code: Select all

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

Re: using regular expressions

Posted: Fri Apr 05, 2019 7:11 am
by AntoninoP
:?:

Personally prefer Debuggex the visual view helps you to understand what regex does.

Re: using regular expressions

Posted: Fri Aug 21, 2020 5:35 am
by Antonio Linares
A tokenizer using regex:

Code: Select all

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

Re: using regular expressions

Posted: Fri Aug 21, 2020 5:41 am
by Antonio Linares
tokenizer with positions

Code: Select all

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

Re: using regular expressions

Posted: Fri Aug 21, 2020 12:00 pm
by AntoninoP

Re: using regular expressions

Posted: Fri Aug 21, 2020 12:09 pm
by Antonio Linares
Antonino,

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

Image

Re: using regular expressions

Posted: Fri Aug 21, 2020 1:06 pm
by cnavarro
A ver si sirve ( cambiar el nombre del fichero PRG que utilizo si no existe )

Code: Select all

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

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

 

Re: using regular expressions

Posted: Fri Aug 21, 2020 1:35 pm
by AntoninoP
Image really?

Re: using regular expressions

Posted: Sat Aug 22, 2020 6:34 am
by Carles
Hi,

Very interesting...

Regards.
C.

Re: using regular expressions

Posted: Sat Aug 22, 2020 7:50 am
by Antonio Linares
Antonino,

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

https://www.modharbour.org/modharbour_s ... 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

Re: using regular expressions

Posted: Sat Aug 22, 2020 11:28 am
by Antonio Linares
modpro.prg bug detected and fixed ;-)

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