# Potential bug in GNAT System.Regexp?

**URL:** https://forum.ada-lang.io/t/potential-bug-in-gnat-system-regexp/1799
**Category:** General
**Tags:** ada, gnat, regexp, aunit
**Created:** [February 11, 2025, 9:33am UTC](https://forum.ada-lang.io/t/potential-bug-in-gnat-system-regexp/1799 "2025-02-11T09:33:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![krischik](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/krischik/32/420_2.png) [@krischik](https://forum.ada-lang.io/u/krischik)
#### Post date: [February 11, 2025, 9:33am UTC](https://forum.ada-lang.io/t/potential-bug-in-gnat-system-regexp/1799/1 "2025-02-11T09:33:31Z")

</div>

I wonder if i found a Bug in GNATs `System.Regexp`. Let me explain.

What I did is make a copy of GNATs `System.Regexp`. My code is GPL so I can do that. Then change the code to be generic. Result is here: [adacl-strings-regexp.ads](https://sourceforge.net/p/adacl/git/ci/553901b20a5f85b3309a20ebf7140ea2a5839a0e/tree/adacl_regexp/src/adacl-strings-regexp.ads) and here [adacl-generic\_strings-regexp.adb](https://sourceforge.net/p/adacl/git/ci/553901b20a5f85b3309a20ebf7140ea2a5839a0e/tree/adacl_regexp/src/adacl-generic_strings-regexp.adb).

Next started to write unit test to ensure that the original and the copy still behave the same. At least for String as my version is now generic and will work for any array of diskrete elements. The tests are here [adacl\_regexp\_test-strings.ads](https://sourceforge.net/p/adacl/git/ci/553901b20a5f85b3309a20ebf7140ea2a5839a0e/tree/adacl_regexp/test/src/adacl_regexp_test-strings.ads) and here [adacl\_regexp\_test-strings.adb](https://sourceforge.net/p/adacl/git/ci/553901b20a5f85b3309a20ebf7140ea2a5839a0e/tree/adacl_regexp/test/src/adacl_regexp_test-strings.adb)

The problem is with test data entry 12:

```Ada
         T.Parameter.Register_Routine (T, Test_01'Access, "12", "Simple |", To_I ("T Pattern X", "T|X Pattern T|X", False, True), True);
         T.Parameter.Register_Routine (T, Test_01'Access, "13", "Insensitive |", To_I ("X PATTERN T", "t|x pattern t|x", False, False), True);

```

The test applying the pattern `T|X Pattern T|X` to the string `T Pattern X` which should match. In the actual test I first check if the old and new code get the same result:

```Ada
      package CL_RegExp renames AdaCL.Strings.RegExp;
      package G_RegExp renames GNAT.RegExp;

      CL_Pattern : constant CL_RegExp.RegExp :=
         CL_RegExp.Compile (Unbounded.To_String (Input.Pattern), Input.Glob, Input.Case_Sensitive);
      G_Pattern : constant G_RegExp.RegExp :=
         G_RegExp.Compile (Unbounded.To_String (Input.Pattern), Input.Glob, Input.Case_Sensitive);
      CL_Actual : constant Boolean := CL_RegExp.Match (Unbounded.To_String (Input.Source), CL_Pattern);
      G_Actual : constant Boolean := G_RegExp.Match (Unbounded.To_String (Input.Source), G_Pattern);
   begin
      Assert_Boolean.Equal
         (Actual => CL_Actual,
          Expected => G_Actual,
          Name => "AdaCL = GNAT");

```

And my code does indeed get the same result. But it’s not the result I’m expecting. Which I test next:

```Ada
      Assert_Boolean.Equal
         (Actual => CL_Actual,
          Expected => Expected,
          Name => "AdaCL Match");
      Assert_Boolean.Equal
         (Actual => G_Actual,
          Expected => Expected,
          Name => "GNAT Match");

```

So either it’s a bug or I misunderstand the regular expression I’m testing. Note test data entry 13, which is the same test but case insensitive does match as expeted.

And ideas what is going on?

---

<div class="post-metadata">

### Author: ![Paul921](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/p/7993a0/32.png) [@Paul921](https://forum.ada-lang.io/u/Paul921)
#### Post date: [February 17, 2025, 4:02pm UTC](https://forum.ada-lang.io/t/potential-bug-in-gnat-system-regexp/1799/2 "2025-02-17T16:02:44Z")

</div>

I think your regular expression should be “(T|X) Pattern (T|X)”.  
See [Regex Tutorial - Alternation with The Vertical Bar](https://www.regular-expressions.info/alternation.html)  
Paul

---

<div class="post-metadata">

### Author: ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)
#### Post date: [February 17, 2025, 6:05pm UTC](https://forum.ada-lang.io/t/potential-bug-in-gnat-system-regexp/1799/3 "2025-02-17T18:05:16Z")

</div>

Welcome to the forums @Paul921!
