Pure Fortran Regular Expression (Regex) Module
Motivation: Why is this needed?
Fortran currently lacks any standard facility for pattern matching and text processing. Researchers and engineers frequently deal with:
- Complex log file parsing from simulation outputs.
- Data cleaning from instrumentation that doesn't follow strict CSV/TSV formats.
- Advanced string validation that goes beyond simple index() and verify() calls.
- While languages like Python (1997), Julia (2012), and Rust (2014) have robust built-in or standard-library regex support, Fortran developers are
forced to write "cascading if-chains" or "manual ASCII arithmetic" for even basic tasks (like finding a digit or optional characters). Providing a native, pure Fortran regex engine fulfills a critical gap in the modern Fortran ecosystem.
Implementation Idea: The Pure Fortran Thompson NFA
I propose implementing a Thompson NFA (Nondeterministic Finite Automaton) engine entirely in Fortran.
Why Pure Fortran? It avoids the "C-interop fragility" of POSIX <regex.h> (which is non-existent on Windows) and adheres to the stdlib principle of zero external dependencies.
Guaranteed Performance: Unlike PCRE (which uses backtracking and can explode to $O(2^n)$ time), Thompson's algorithm guarantees linear time $O(n \times m)$ relative to input length.
Zero Dependencies: Fully portable across gfortran, ifx, flang, and nvfortran without linking external libraries like PCRE2.
The engine will follow a 4-stage pipeline:
Lexer: Tokenize the pattern string into atoms (literals, operators, escapes). Parser: Build an Abstract Syntax Tree (AST) using recursive descent. NFA Builder: Convert the AST into a state graph using Thompson's construction. Executor: Simulate the NFA on the input string using a linear-time set-based simulation.
Prior Art
Go (golang/regexp): The regexp package in Go is the gold standard for a production-grade Thompson NFA implementation. We will use its API (Find, Match, Replace) as a blueprint for the Fortran interface.
Russ Cox : The definitive work proving that Thompson NFA is the superior choice for predictable performance.
Additional Information
Initial Scope: Support for *, +, ?, |, (), [], ^, $.
Memory Management: The engine will leverage modern Fortran's allocatable arrays for state management to ensure safety and efficiency.
Some useful references of regex packages developed in the community :
https://fortran-lang.discourse.group/t/fortran-regex-library/4917 https://fortran-lang.discourse.group/t/regular-expressions-regex-and-fortran/778 https://fortran-lang.discourse.group/t/new-release-of-forgex-fortran-regular-expression-v3-4/8325
Thanks @jalvesz, for the links!
My goal with this proposal is to implement a Thompson NFA engine (similar to Go's regexp) to ensure a portable, zero-dependency, and linear-time $O(n \times m)$ solution for stdlib. This avoids the performance issues (like catastrophic backtracking) that some other libraries might have. I’ll open a draft PR with initial specifications and NFA engine.
I have been thinking about something like this for years, but never got around to it. My latest attempt was based on https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html. That describes a very basic regular expression matcher, but the thing that was really trciky is determining the end (!) of the matching substring. Then when I tried to build it via fpm, I found out that there were array bound violations (srting indices, actually). I still think something like this is useful as it is easy to understand ;).
My more ambitious goal was based on work by Russ Cox, https://swtch.com/~rsc/regexp/. There I got stuck with some C idiom that I failed to understand.
Perhaps these references are of use to you as well. If I find the time, among all the fun projects I have wandering though my head and occasionally demanding attention, then I will work on this topic again.
Op di 31 mrt 2026 om 09:34 schreef JAYA SATHVIK TANGA < @.***>:
JAi-SATHVIK left a comment (fortran-lang/stdlib#1163) https://github.com/fortran-lang/stdlib/issues/1163#issuecomment-4160544941
Thanks @jalvesz https://github.com/jalvesz, for the links! My goal with this proposal is to implement a Thompson NFA engine (similar to Go's regexp) to ensure a portable, zero-dependency, and linear-time $O(n \times m)$ solution for stdlib. This avoids the performance issues (like catastrophic backtracking) that some other libraries might have. I’ll open a draft PR with initial specifications and NFA engine.
— Reply to this email directly, view it on GitHub https://github.com/fortran-lang/stdlib/issues/1163?email_source=notifications&email_token=AAN6YR73AWT2QY47QSY2RZ34TNYHZA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJWGA2TINBZGQY2M4TFMFZW63VKON2WE43DOJUWEZLEUVSXMZLOOSWGM33PORSXEX3DNRUWG2Y#issuecomment-4160544941, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN6YR5ZGLWYM5N3GXZBKJD4TNYHZAVCNFSM6AAAAACXDKATMGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCNRQGU2DIOJUGE . You are receiving this because you are subscribed to this thread.Message ID: @.***>
Thanks @arjenmarkus, for the encouragement! It's great to hear we're aligned on the Russ Cox / Thompson NFA approach. I’m specifically focusing on those tricky 'match end' boundaries and avoiding the array-bound issues you mentioned by using modern, idiomatic Fortran. I’ll be opening a draft PR soon and would love your feedback when you have a moment!
Hi Jaya,
that is good to hear. I will have a look at your code!
Op di 31 mrt 2026 om 20:46 schreef JAYA SATHVIK TANGA < @.***>:
JAi-SATHVIK left a comment (fortran-lang/stdlib#1163) https://github.com/fortran-lang/stdlib/issues/1163#issuecomment-4164680724
Thanks @arjenmarkus https://github.com/arjenmarkus, for the encouragement! It's great to hear we're aligned on the Russ Cox / Thompson NFA approach. I’m specifically focusing on those tricky 'match end' boundaries and avoiding the array-bound issues you mentioned by using modern, idiomatic Fortran. I’ll be opening a draft PR soon and would love your feedback when you have a moment!
— Reply to this email directly, view it on GitHub https://github.com/fortran-lang/stdlib/issues/1163?email_source=notifications&email_token=AAN6YR5Q4QDDCHW7LUGC5KL4TQHAPA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTIMJWGQ3DQMBXGI2KM4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-4164680724, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN6YR72UFUY5I2XUHV7R4D4TQHAPAVCNFSM6AAAAACXDKATMGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DCNRUGY4DANZSGQ . You are receiving this because you were mentioned.Message ID: @.***>