Regular expressions (regex) and Fortran

Regular expressions (regex) are characters sequences defining patterns which are used for searching, replacing in strings, or validating strings (e.g. is it a valid e-mail address?)… Many languages give access to regular expressions: Perl (whose library has become a reference), Python, C, C++, Java, Javascript, Ruby, TCL, PHP, Julia… And Fortran can also be interfaced with regex libraries.

I have just added a short regex example in gtk-fortran in the gtk3 and gtk4 branches. That example don’t use GTK itself (no GUI), but just some GLib functions:
https://github.com/vmagnin/gtk-fortran/blob/gtk3/examples/regex.f90
As you may know, GTK is based on the general purpose GLib library, which includes a regex API based on the PCRE library:
https://developer.gnome.org/glib/stable/glib-Perl-compatible-regular-expressions.html

In the fpm registry, you can also find (by @urbanjost):

See also its M_REGEX(3fm) for calling the POSIX Regular Expression library:
https://github.com/urbanjost/general-purpose-fortran

@interkosmos also proposes interfaces toward POSIX regular expressions:

Finally, some other Fortran regex codes can be found in GitHub:
https://github.com/search?q=fortran+regex

2 Likes

I have tried to implement a regular expressions engine in Fortran in the past, based on the excellent exposé by Russ Cox (Implementing Regular Expressions). Unfortunately, my attempts have been thwarted by my lack of patience. The design as described always seemed simple enough.

The one Fortran implementation that I found on github using the above search string is incomplete. It may parse the expression itself, but it does not parse any strings.

Of course, a library that wraps an existing, well-designed and implemented library is very nice to have, but it creates an extra dependency. Perhaps I should muster more patience …

2 Likes

There is also:

used in fpm-search.