# Regular expressions (regex) and Fortran

**URL:** <https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778>\
**Category:** Uncategorized\
**Created:** [March 4, 2021, 6:03pm UTC](https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778 "2021-03-04T18:03:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 4, 2021, 6:03pm UTC](https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778/1 "2021-03-04T18:03:00Z")

</div>

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression) (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](https://github.com/vmagnin/gtk-fortran/blob/gtk3/examples/regex.f90)  
As you may know, GTK is based on the general purpose [GLib library](https://en.wikipedia.org/wiki/GLib), which includes a regex API based on the [PCRE library](http://www.pcre.org/):  
[https://developer.gnome.org/glib/stable/glib-Perl-compatible-regular-expressions.html](https://developer.gnome.org/glib/stable/glib-Perl-compatible-regular-expressions.html)

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

> **[urbanjost/M\_match](https://github.com/urbanjost/M_match)**
>
> subset of Regular Expressions implemented in Fortran

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

@interkosmos also proposes interfaces toward POSIX regular expressions:

> **[interkosmos/fortran-unix](https://github.com/interkosmos/fortran-unix)**
>
> Fortran 2008 interface bindings to selected POSIX and SysV procedures

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

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 5, 2021, 12:11pm UTC](https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778/2 "2021-03-05T12:11:25Z")

</div>

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](https://swtch.com/~rsc/regexp/)). 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 …

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 13, 2021, 8:05pm UTC](https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778/3 "2021-03-13T20:05:41Z")

</div>

There is also:

> **[ZedThree/fortran\_pcre](https://github.com/ZedThree/fortran_pcre)**
>
> Fortran wrapper around the PCRE library. Contribute to ZedThree/fortran\_pcre development by creating an account on GitHub.

used in [fpm-search](https://github.com/brocolis/fpm-search).
