using regular expressions

Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

using regular expressions

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: using regular expressions

Post by AntoninoP »

:?:

Personally prefer Debuggex the visual view helps you to understand what regex does.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: using regular expressions

Post 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
regards, saludos

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

Re: using regular expressions

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: using regular expressions

Post by AntoninoP »

User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: using regular expressions

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
cnavarro
Posts: 5792
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: using regular expressions

Post 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

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

 
C. Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
Si alguien te dice que algo no se puede hacer, recuerda que esta hablando de sus limitaciones, no de las tuyas.
AntoninoP
Posts: 347
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy
Contact:

Re: using regular expressions

Post by AntoninoP »

Image really?
User avatar
Carles
Posts: 937
Joined: Fri Feb 10, 2006 2:34 pm
Location: Barcelona
Contact:

Re: using regular expressions

Post by Carles »

Hi,

Very interesting...

Regards.
C.
Salutacions, saludos, regards

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

https://modharbour.app
https://modharbour.app/compass
https://forum.modharbour.app
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: using regular expressions

Post 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
regards, saludos

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

Re: using regular expressions

Post by Antonio Linares »

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
Post Reply