FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Expresiones regulares
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Expresiones regulares
Posted: Thu Jun 04, 2015 05:01 PM
Antonio L. y amigos:

Que tan factible es elaborar un analizador de expresiones regulares? o ya existe?

Code (fw): Select all Collapse
https://msdn.microsoft.com/es-mx/es/library/az24scfc(v=vs.110).aspx


Mi inquietud surge de la necesidad de validar las direcciones de correo electr贸nico,
entre otros textos.

Saludos
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 817
Joined: Sun Jun 15, 2008 07:47 PM
Re: Expresiones regulares
Posted: Thu Jun 04, 2015 09:11 PM

Hola Armando :-)

脡chale un vistazo a harbour\tests\regex.prg

______________________________________________________________________________

Sevilla - Andaluc铆a
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Expresiones regulares
Posted: Thu Jun 04, 2015 10:40 PM

xmanuel:

Gracias por el consejo, pero no tengo ese prg, cuento FWH1412 y Harbour1412 pero no
existe ese prg, ya lo busque en todo mi DD y no hay de pi帽a. :oops:

Saludos

SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
Posts: 1364
Joined: Wed Jun 21, 2006 12:39 AM
Re: Expresiones regulares
Posted: Fri Jun 05, 2015 01:08 AM
Desde el arc贸n de los recuerdos te posteo el c贸digo de regex.prg que pertenece a xHarbour ( no s茅 cual versi贸n )

Code (fw): Select all Collapse
* regex.prg
* $Id: regex.prg,v 1.4 2003/05/27 20:05:54 jonnymind Exp $
* Test for regular expression functions
*
* Giancarlo Niccolai
*

PROCEDURE Main()
聽 聽LOCAL regex
聽 聽LOCAL aMatch
聽 聽LOCAL cStr, nRow := 2, nCol
聽 聽LOCAL aSource := { ;
聽 聽 聽 "First date to match: 2001-3-21", ;
聽 聽 聽 "2002-12/2", ;
聽 聽 聽 "Another can be 1999/5/12, and suceeds", ;
聽 聽 聽 "Could be 1999/534/12, but this will fail" ;
聽 聽}

聽 聽CLEAR SCREEN

聽 聽@0,15 SAY "X H A R B O U R - Regular expression scan tests"
聽 聽/*
聽 聽* Standard regex to get the ISO date format:
聽 聽* ([0-9]{4}): exactly fuor digits (year); it is in brackets,
聽 聽* 聽 聽this means that we want it back as a group
聽 聽* [-/]: one bar or a minus
聽 聽* ([0-9]{1,2}): one or two digits
聽 聽*/
聽 聽regex := HB_RegexComp( "([0-9]{4})[-/]([0-9]{1,2})[-/]([0-9]{1,2})" )

聽 聽FOR EACH cStr IN aSource
聽 聽 聽 @nRow, 5 SAY "String is '" + cStr + "'"
聽 聽 聽 nRow ++
聽 聽 聽 aMatch := HB_Regex( regex, cStr )
聽 聽 聽 IF .not. Empty( aMatch )
聽 聽 聽 聽 聽@nRow, 10 SAY "Matched: " + aMatch[1] + " ( Year: " + aMatch[2] + ", Month: " +;
聽 聽 聽 聽 聽 聽 aMatch[3] + ", Day: " + aMatch[4] + ")"
聽 聽 聽 ELSE
聽 聽 聽 聽 聽@nRow, 10 SAY "Match FAILED!"
聽 聽 聽 ENDIF
聽 聽 聽 nRow += 2
聽 聽NEXT

聽 聽cStr := "searching 'regex' here:"
聽 聽@nRow, 5 SAY "A test of a regex compiled on the fly; " + cStr
聽 聽aMatch := HB_Regex( "(.*)regex(.*)", cStr )
聽 聽nRow++
聽 聽IF Empty( aMatch )
聽 聽 聽 @ nRow, 10 SAY "NOT FOUND!"
聽 聽ELSE
聽 聽 聽 @ nRow, 10 SAY "Found (Before: <<" + aMatch[2] + ">>, After: <<" + aMatch[3] + ">>)"
聽 聽ENDIF

聽 聽nRow+=2

聽 聽cStr := "A str; with: separators :; here "
聽 聽@nRow, 5 SAY "Split test; splitting '" + cStr + "' by ':|;'"
聽 聽nRow++
聽 聽aMatch := HB_RegexSplit( ":|;", cStr )
聽 聽IF Empty( aMatch )
聽 聽 聽 @nRow++, 10 SAY "Test failed"
聽 聽ELSE
聽 聽 聽 nCol := 10
聽 聽 聽 FOR EACH cStr in aMatch
聽 聽 聽 聽 聽@nRow, nCol SAY cStr + "/"
聽 聽 聽 聽 聽nCol += Len( cStr ) +1
聽 聽 聽 NEXT
聽 聽 聽 nRow++
聽 聽ENDIF

聽 聽cStr := "A string without separators"
聽 聽@nRow, 5 SAY "Split test; splitting '" + cStr + "' by ':|;'"
聽 聽nRow++
聽 聽aMatch := HB_RegexSplit( ":|;", cStr )
聽 聽IF Empty( aMatch )
聽 聽 聽 @nRow++, 10 SAY "Test failed"
聽 聽ELSE
聽 聽 聽 nCol := 10
聽 聽 聽 FOR EACH cStr in aMatch
聽 聽 聽 聽 聽@nRow, nCol SAY cStr + "/"
聽 聽 聽 聽 聽nCol += Len( cStr ) +1
聽 聽 聽 NEXT
聽 聽 聽 nRow++
聽 聽ENDIF

聽 聽cStr := "Test for RegexAtx"
聽 聽@nRow, 5 SAY "RegexAtx test; scanning '" + cStr + "' by 'Reg(.x)'"
聽 聽nRow++
聽 聽aMatch := HB_RegexAtx( "Reg(.x)", cStr )
聽 聽IF Empty( aMatch )
聽 聽 聽 @nRow++, 10 SAY "Test failed"
聽 聽ELSE
聽 聽 聽 nCol := 15
聽 聽 聽 FOR EACH cStr in aMatch
聽 聽 聽 聽 聽@nRow, nCol SAY "FOUND: '"+cStr[1] + "' Start: " + AllTrim( Str(cStr[2]) )+;
聽 聽 聽 聽 聽 聽 " End: " + Alltrim( Str( cStr[3] ) )
聽 聽 聽 聽 聽nRow++
聽 聽 聽 NEXT
聽 聽 聽 nRow++
聽 聽ENDIF

聽 聽@nRow, 1
聽 聽@24, 25 SAY "Press a key to continue"
聽 聽Inkey( 0 )
RETURN


Espero te sirva. Saludos
Posts: 3358
Joined: Fri Oct 07, 2005 08:20 PM
Re: Expresiones regulares
Posted: Fri Jun 05, 2015 02:45 AM

Horacio:

Gracias, voy a probarlo a ver si lo entiendo :)

O si alguien puede explicarme su funcionalidad?

Saludos

SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero

Continue the discussion