"Replace Function" with "regular expression"

New to FreeBASIC? Post your questions here.
Makoto WATANABE
Posts: 105
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

"Replace Function" with "regular expression"

Postby Makoto WATANABE » Sep 22, 2016 1:48

I want "Replace Function" with "regular expression".
I want to use $1 $2 etc. as replace_pattern.
$1 is the first match and $2 is the second match etc.
http://www.javascriptkit.com/javatutors/re3.shtml

I feel that
examples/regex/TRE/replace.bas
can not cope "portion (match)"

Please teach "Replace Function" that I can use in FreeBASIC.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 7:27

Hello!

There is an example that does what you want in FBC\examples\regex\PCRE. The file is named preg_class.bas.

Unfortunately, I don't succeed to compile it with FBC 1.05.0 for Win32. I get this error:

preg_class.o:fake:(.text+0x1eb): undefined reference to `_imp__pcre_free'
preg_class.o:fake:(.text+0x41d): undefined reference to `_imp__pcre_free'
preg_class.o:fake:(.text+0x8d4): undefined reference to `_imp__pcre_malloc'
preg_class.o:fake:(.text+0x90f): undefined reference to `_imp__pcre_free'


I use a file libpcre.a downloaded here. The link was given in this discussion.

It's weird, because I can compile all my other examples using PCRE. And I remember that some time ago I could compile also that example.
Last edited by Roland Chastain on Sep 22, 2016 12:37, edited 1 time in total.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 8:02

By the way, I don't succeed to build CRegex static library (in examples\PCRE\CRegex). I know I succeeded to build it in 2014, because I still have the file (libcregex.a). But even with that file I can't compile the example test.bas.

CRegex.bi(68) error 14: Expected identifier, found 'pcre_extra_' in 'as pcre_extra_ ptr extra'
Makoto WATANABE
Posts: 105
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Re: "Replace Function" with "regular expression"

Postby Makoto WATANABE » Sep 22, 2016 11:54

Thanks for your reply.
Please teach me the procedure for compiling the "preg_class.bas" a little more concretely.

In my environment, there was already "libpcre.a" (278KB 2013/05/13).
C: \Tool\FreeBASIC\lib\win32\libpcre.a

I replaced it with the "libpcre.a" (721KB 2012/05/19) of the link which you told.

However, the situation becomes the same error.
'preg_class.o:fake:(.text+0x1eb): undefined reference to `_imp__pcre_free'
'preg_class.o:fake:(.text+0x41d): undefined reference to `_imp__pcre_free'
'preg_class.o:fake:(.text+0x8d4): undefined reference to `_imp__pcre_malloc'
'preg_class.o:fake:(.text+0x90f): undefined reference to `_imp__pcre_free'

And, "libpcre.dll.a" is not found in my environment.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 12:32

Makoto WATANABE wrote:Thanks for your reply.
Please teach me the procedure for compiling the "preg_class.bas" a little more concretely.


Sorry, but for me I already told you all what I know. I am sure I already compiled the "preg_class.bas" example (using the same *.a file). It was in january 2015. I don't know why I can't compile it now. I hope someone will give us the explanation.
St_W
Posts: 1464
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: "Replace Function" with "regular expression"

Postby St_W » Sep 22, 2016 13:06

I built libpcre some time ago:
http://users.freebasic-portal.de/stw/fi ... ingw32.zip

Unfortunately I haven't written down how I did it back then and haven't saved the build environment (or at least I can't find it).
Last edited by St_W on Sep 22, 2016 13:12, edited 1 time in total.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 13:11

St_W wrote:I built libpcre some time ago:
http://users.freebasic-portal.de/stw/fi ... ingw32.zip


Yes, I tried your file but the result is the same.
St_W
Posts: 1464
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: "Replace Function" with "regular expression"

Postby St_W » Sep 22, 2016 13:17

Roland Chastain wrote:
St_W wrote:I built libpcre some time ago:
http://users.freebasic-portal.de/stw/fi ... ingw32.zip


Yes, I tried your file but the result is the same.

Strange, it did work for me.
Are you sure that the library is used, and not some in FreeBasic's lib folder or elsewhere? (simple remove the libpcre.a and check whether the compiler complains like this "ld.exe: cannot find -lpcre")

btw I've installed an older FreeBasic, fbc 1.04.0, on this PC - maybe that's the difference?

//edit: sorry, I tried "pcredemo.bas" instead of "preg_class.bas"

//edit2: no, that wasn't the problem. Both "pcredemo.bas" AND "preg_class.bas" work for me.

btw you just need the "libpcre.a" - I don't know why I included the other files in the archive.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 13:23

St_W wrote:Strange, it did work for me.
Are you sure that the library is used, and not some in FreeBasic's lib folder or elsewhere? (simple remove the libpcre.a and check whether the compiler complains like this "ld.exe: cannot find -lpcre")


Yes, I already checked that.

St_W wrote:btw I've installed an older FreeBasic, fbc 1.04.0, on this PC - maybe that's the difference?


I've just made a test with FBC 1.00.0, and it works. So, indeed, the problem seems to be related to FBC 1.05.0.
srvaldez
Posts: 1879
Joined: Sep 25, 2005 21:54

Re: "Replace Function" with "regular expression"

Postby srvaldez » Sep 22, 2016 13:27

also, remember that FB by default links against the dll import lib, you may need to use -static
St_W
Posts: 1464
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: "Replace Function" with "regular expression"

Postby St_W » Sep 22, 2016 13:58

The difference between 1.04 and 1.05 are the new pcre headers, which cause the compilation failure. Maybe the compiled libpcre is tool old?

//edit: the solution is simple:
Put

Code: Select all

#Define PCRE_STATIC
before your pcre.bi include.

//edit2:
I built and uploaded the latest pcre version for win32/win64:
http://users.freebasic-portal.de/stw/fi ... static.zip

build is simple with cmake+mingw; I've disabled all 3rd party library dependencies - if you need some of those addition features you've to compile pcre yourself.
caseih
Posts: 1334
Joined: Feb 26, 2007 5:32

Re: "Replace Function" with "regular expression"

Postby caseih » Sep 22, 2016 15:10

I just tried the preg_class.bas file on my Linux 64-bit machine and it seems to go into an endless loop that accumulates more and more memory until the OS kills it when it runs out of memory. I just compiled it with:
fbc preg_class.bas

I got warnings about incompatible pointer types. I had a hunch that either the .bi file nor the preg_class.bas file is not 64-bit compatible. Looking at the preg_class.bas file, I see that it's using Integer types, where it most certainly should be using Long, on Linux anyway. This is with 1.05 of the compiler.

Edit: Yes the problem was in preg_class.bas. It is using integer in a few places where it should have been long. No more infinite loop and crash with out of memory now.
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 19:49

St_W wrote:Put

Code: Select all

#Define PCRE_STATIC
before your pcre.bi include.


Problem solved. Thank you (also for the latest PCRE version).
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

Re: "Replace Function" with "regular expression"

Postby Roland Chastain » Sep 22, 2016 20:05

preg_class.bas is more than an example. It's a small library! It would deserve a .bi file. :)

P.-S. Done. :)

Code: Select all

' preg_class.bi

declare function preg_match_simple(pattern as zstring ptr, subject as zstring ptr, byval offset as integer = 0, byval flags as integer = 0) as string
declare function preg_replace_simple(pattern as zstring ptr, replacement as zstring ptr, subject as zstring ptr, byval offset as integer = 0, byval flags as integer = 0) as string

#include "preg_class.bas"


Code: Select all

' preg_class_test.bas

#include "preg_class.bi"

? preg_replace_simple("(\w+) (\w+)", "$2 $1", "Bond James")

sleep
Makoto WATANABE
Posts: 105
Joined: Apr 10, 2010 11:41
Location: Japan
Contact:

Thanks : "Replace Function" with "regular expression"

Postby Makoto WATANABE » Sep 23, 2016 13:37

When I execute "preg_class_test.bas" , "James Bond" appears !

I learned the usage of preg_class.bas("Replace Function" with "regular expression").
And I learned an usage of the "HeaderFile" !

Thank you everyone !

I was introduced your programs to Japanese people.
http://makoto-watanabe.main.jp/freebasic/tipsRegExp.html#replace
Please consent to this.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests