Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A Fortran regular-expression-matching function similar to INDEX?

636 views
Skip to first unread message

Myron A. Calhoun

unread,
Nov 20, 2009, 8:38:18 PM11/20/09
to
I need a Fortran (preferably FORTRAN!-) function similar to INDEX:

J = INDEX (String, Substring)

except Substring could be a regular expression (even relatively-simple
regular expressions).

I've "Googled" all sorts of web-searches but haven't found anything.
Surely someone has done this?
--
--Myron A. Calhoun.
Five boxes preserve our freedoms: soap, ballot, witness, jury, and cartridge
NRA Life Member & Certified Instructor for Rifle, Pistol, & Home Firearm Safety
Also Certified Instructor for the Kansas Concealed-Carry Handgun (CCH) license

glen herrmannsfeldt

unread,
Nov 20, 2009, 9:11:23 PM11/20/09
to
Myron A. Calhoun <mcal...@sdf.lnoonsepsatmar.org> wrote:
> I need a Fortran (preferably FORTRAN!-) function similar to INDEX:

> J = INDEX (String, Substring)

> except Substring could be a regular expression (even relatively-simple
> regular expressions).

Well, you could call the C routines. The regexp routines I know
of are in C. I believe that there are a few open source freeware
regexp packages. There is at least one from GNU for the GNU
utilities.



> I've "Googled" all sorts of web-searches but haven't found anything.
> Surely someone has done this?

They go by regexp, with regcomp being the compiler and regexec
doing the search.

-- glen

Arjen Markus

unread,
Nov 21, 2009, 8:56:17 AM11/21/09
to
On 21 nov, 02:38, mcalh...@sdf.lNoOnSePsAtMar.org (Myron A. Calhoun)
wrote:

> I need a Fortran (preferably FORTRAN!-) function similar to INDEX:
>
>    J = INDEX (String, Substring)
>
> except Substring could be a regular expression (even relatively-simple
> regular expressions).
>
> I've "Googled" all sorts of web-searches but haven't found anything.
> Surely someone has done this?

If your regular expressions are simple enough (include only "a single
arbitrary
character" or "any number of arbitrary characters") - so a simplified
form
of glob matching, like with files, then you might find the module in
my Flibs project useful (http://flibs.sf.net).

You might even use the Fortran-generating version of the lemon parser
generator
useful, but that is a bit roundabout.

Regards,

Arjen

frank

unread,
Nov 21, 2009, 7:21:40 PM11/21/09
to
On Sat, 21 Nov 2009 02:11:23 +0000, glen herrmannsfeldt wrote:

>> J = INDEX (String, Substring)
>
>> except Substring could be a regular expression (even relatively-simple
>> regular expressions).
>
> Well, you could call the C routines. The regexp routines I know of are
> in C. I believe that there are a few open source freeware regexp
> packages. There is at least one from GNU for the GNU utilities.

index is a C bsd-ism and an example of something that you can't really do
in fortran, at least in one expression.
--
frank

"Guns: yes, they are harmful."

mecej4

unread,
Nov 21, 2009, 9:29:30 PM11/21/09
to
frank wrote:

The Fortran 90+ intrinsic function 'index' (see above) is similar to but not
quite the same as the C function char *index(const char *s, int c). The
Fortran function is actually closer to the C library function

char *strstr(const char *haystack, const char *needle)

in that they are both useful to locate a substring in a string.

-- mecej4

mecej4

unread,
Nov 22, 2009, 7:34:47 PM11/22/09
to
Myron A. Calhoun wrote:

> I need a Fortran (preferably FORTRAN!-) function similar to INDEX:
>
> J = INDEX (String, Substring)
>
> except Substring could be a regular expression (even relatively-simple
> regular expressions).
>
> I've "Googled" all sorts of web-searches but haven't found anything.
> Surely someone has done this?

Here is an example of using the Regex(3) routines from GFortran in Linux.

The routines regcompF, regexecF and regfreeF are Fortran wrappers around the
calls to the routines in the C library.

Alternatively, if you have the ISO-C-string module, you may be able to skip
using wrappers, but I am not sure that the ISO-C-string module is able to
handle declarations of structures of type regex_t (defined in regex.h and
shared among the wrapper routines).

This example uses a regular expression search for vowels. Those found are
replaced by an underscore.

HTH.

-- mecej4

___________________________________________________________

program useregex
character(len=40) :: str='Able was I ere I saw Elba'
character(len=7) :: regex='[aeiou]'
integer :: cflags=0,eflags=0
integer :: i,j
!
integer,dimension(2) :: rm
!
rv=regcompF(regex,cflags) ! compile the REGEXP
i=1
do while(i <= len_trim(str))
rv=regexecF(str(i:),1,rm,eflags) ! find matching position
j=i+rm(1)
str(j:j)='_' ! change matched char
i=i+rm(2) ! new index at which to search
end do
call regfreeF()
write(*,'("converted string: ",A)')str
end program useregex
----------------------------
converted string: Abl_ w_s I _r_ I s_w Elb_

0 new messages