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
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
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
>> 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."
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
> 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_