# Regex usage in TSystem::ListLibraries()

**URL:** <https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503>\
**Category:** Newbie\
**Created:** [November 24, 2022, 12:25pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503 "2022-11-24T12:25:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![abelloni](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@abelloni](https://root-forum.cern.ch/u/abelloni)\
**Post date:** [November 24, 2022, 12:25pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/1 "2022-11-24T12:25:48Z")

</div>

Dear all,  
I am afraid that my problem has a simple solution, but I failed to find it. After loading some libraries with:

```auto
gSystem->Load("myLib.so");

```

I want to check the actual location of the loaded libraries; I am trying to accomplish this by using TSystem::ListLibraries(const char \*regex = “”), but with partial success. I.e., if I use:

```auto
gSystem->ListLibraries();

```

I obtain a complete list of all the loaded libraries. If I replace “” with any kind of regular expression, the result is always empty. For example:

```auto
gSystem->ListLibraries(".*")

```

returns 0 matches, while I naively expected its output to be the same as in the first line. I see this behavior even when I do not load any library, and start root with the -n option (to skip the rootlogon.C file). Would it be possible to point me to some successful examples of using the regex parameter? Thanks for any suggestions! Best,

Alberto

* * *

_ROOT Version:_ 6.26/08  
_Platform:_ x86\_64  
_Compiler:_ gcc 11.3.0 (probably)

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2022, 1:13pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/2 "2022-11-24T13:13:30Z")

</div>

I agree with you. The regular expression does not seem to work as it should.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 24, 2022, 5:52pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/3 "2022-11-24T17:52:32Z")

</div>

I investigated a bit further, and it seems that the “/” characters in the libraries’ names (path names) make the RexExp inoperant. I will see how to fix that and propose a PR. Thanks to have seen it.

---

<div class="post-metadata">

**Author:** ![abelloni](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@abelloni](https://root-forum.cern.ch/u/abelloni)\
**Post date:** [November 24, 2022, 8:29pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/4 "2022-11-24T20:29:18Z")

</div>

Hello,

thank you very much for looking into this, and the extremely prompt feedback! Best,

Alberto

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 6:49am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/5 "2022-11-25T06:49:57Z")

</div>

The following script shows the issue. The string `s1` contains a `/` and `Index` with a regulars expression `*` does not work whereas with `s2`, which has no `/`, it is fine:

```auto
void regexptest() {
   TRegexp re("*", kTRUE);

   TString s1 = "lib/libRIO.so";
   printf("s1: %s %d\n", s1.Data(), s1.Index(re));

   TString s2 = "lib-libRIO.so";
   printf("s2: %s %d\n", s2.Data(), s2.Index(re));
}

```

gives:

```auto
root [2] .x regexptest.C
s1: lib/libRIO.so -1
s2: lib-libRIO.so 0

```

-1 means “no match”, and 0 means “match”.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 8:27am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/6 "2022-11-25T08:27:14Z")

</div>

`TRegexp re(".*", kFALSE);`

[TRegexp::MakeWildcard](https://root.cern/doc/master/classTRegexp.html#ab2762d059ace7f79a45b76b0f3b1a1b1)

[TSystem::ListLibraries](https://root.cern/doc/master/classTSystem.html#a7f98c9625a54c9553862bbbaf0e7709b)

@abelloni The name of the parameter is misleading. It’s not a “regexp” but a “wildcard expression” (with special treatment of “/”).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 8:48am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/7 "2022-11-25T08:48:30Z")

</div>

Thanks @Wile_E_Coyote . That helps. I will check further this track. In the current implementation `TSystem::ListLibraries` calls:

```auto
      TRegexp user_re(regexp, kTRUE);

```

With that, it does not work:

```auto
% root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01 https://root.cern |
  | (c) 1995-2022, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for macosx64 on Nov 24 2022, 06:43:42 |
  | From heads/master@v6-25-02-2828-g33beda7d0d |
  | With Apple clang version 14.0.0 (clang-1400.0.29.202) |
  | Try '.help'/'.?', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

root [0] gSystem->ListLibraries();
 
Loaded shared libraries
=======================
/Users/couet/git/couet-root-bin/lib/libRint.so
/Users/couet/git/couet-root-bin/lib/libCore.so
/Users/couet/git/couet-root-bin/lib/libRIO.so
/Users/couet/git/couet-root-bin/lib/libThread.so
/Users/couet/git/couet-root-bin/lib/libtbb.dylib
/Users/couet/git/couet-root-bin/lib/libCling.so
/Users/couet/git/couet-root-bin/lib/libMathCore.so
/Users/couet/git/couet-root-bin/lib/libImt.so
/Users/couet/git/couet-root-bin/lib/libMultiProc.so
/Users/couet/git/couet-root-bin/lib/libNet.so
-----------------------
10 libraries loaded
=======================

root [1] gSystem->ListLibraries("*");
 
Loaded shared libraries
=======================
-----------------------
0 libraries loaded
=======================

root [2] gSystem->ListLibraries(".*");
 
Loaded shared libraries
=======================
-----------------------
0 libraries loaded
=======================

root [3] gSystem->ListLibraries(".*RIO.*");
 
Loaded shared libraries
=======================
-----------------------
0 libraries loaded
=======================
root [4] 

```

I will try with `kFALSE`.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 8:51am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/8 "2022-11-25T08:51:52Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> @abelloni The name of the parameter is misleading. It’s not a “regexp” but a “wildcard expression” (with special treatment of “/”).

@Wile_E_Coyote , yes but how to list only `RIO` lib only in that case?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 8:52am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/9 "2022-11-25T08:52:56Z")

</div>

@couet Please note that the [TSystem::ListLibraries](https://root.cern/doc/master/classTSystem.html#a7f98c9625a54c9553862bbbaf0e7709b) method description explicitly says that it expects a “wildcard expression”, not a “regexp”. That’s how it always was. Don’t make changes that will break “backward compatibility”.

I don’t really know how to force ROOT “wildcard expression” to treat “/” as a regular character. Maybe the original author of this code knows it.

I would use [TSystem::GetLibraries](https://root.cern/doc/master/classTSystem.html#ad340f02c58426db03aefc78f73f5cded), with an empty “regexp” parameter, and then parse the returned character string myself (with a regular “regexp”, not a “wildcard expression”).

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 9:37am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/11 "2022-11-25T09:37:59Z")

</div>

Changing the flag to kFALSE in GetLibraries make ListLibrarie mush easier to use.

```auto
root [2] gSystem->ListLibraries(".*");
 
Loaded shared libraries
=======================
/Users/couet/git/couet-root-bin/lib/libRint.so
/Users/couet/git/couet-root-bin/lib/libCore.so
/Users/couet/git/couet-root-bin/lib/libRIO.so
/Users/couet/git/couet-root-bin/lib/libThread.so
/Users/couet/git/couet-root-bin/lib/libtbb.dylib
/Users/couet/git/couet-root-bin/lib/libCling.so
/Users/couet/git/couet-root-bin/lib/libMathCore.so
/Users/couet/git/couet-root-bin/lib/libImt.so
/Users/couet/git/couet-root-bin/lib/libMultiProc.so
/Users/couet/git/couet-root-bin/lib/libNet.so
-----------------------
10 libraries loaded
=======================
root [3] gSystem->ListLibraries(".*RIO.*");
 
Loaded shared libraries
=======================
/Users/couet/git/couet-root-bin/lib/libRIO.so
-----------------------
1 libraries loaded
=======================
root [4] 

```

But I won’t do that change …

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 10:19am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/12 "2022-11-25T10:19:20Z")

</div>

```cpp
//
// Usage example:
// root -l -q 'ListLibraries.cxx(".*libCore\\..*")'
//
#include "TString.h"
#include "TRegexp.h"
#include "TSystem.h"
#include <iostream>
void ListLibraries(const char *regexp = ".*", Bool_t wildcard = kFALSE) {
  if (!gSystem) return; // just a precaution
  if (!(regexp && regexp[0])) regexp = (wildcard ? "*" : ".*");
  TRegexp pat(regexp, wildcard);
  TString libs(gSystem->GetLibraries());
  TString tok;
  Ssiz_t from = 0, ext;
  while (libs.Tokenize(tok, from, " ")) {
    if ((tok.Index(pat, &ext) != 0) || (ext != tok.Length())) continue;
    std::cout << tok << "\n";
  }
}

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 10:38am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/14 "2022-11-25T10:38:20Z")

</div>

@couet How about something that is “backward compatible”:

```cpp
void TSystem::ListLibraries(const char *regexp = "", Bool_t wildcard = kTRUE)
{
   // ...
   TString libs = GetLibraries(regexp, wildcard);
   // ...
}
```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 10:47am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/15 "2022-11-25T10:47:18Z")

</div>

Yes I was thinking of it.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 3:20pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/16 "2022-11-25T15:20:55Z")

</div>

I made a [PR](https://github.com/root-project/root/pull/11776).

This PR allows the following:

```auto
root [0] gSystem->ListLibraries("Core")
 
Loaded shared libraries
=======================
-----------------------
0 libraries loaded
=======================

root [1] gSystem->ListLibraries("Core",kFALSE)
 
Loaded shared libraries
=======================
/Users/couet/git/couet-root-bin/lib/libCore.so
/Users/couet/git/couet-root-bin/lib/libMathCore.so
-----------------------
2 libraries loaded
=======================

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 3:29pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/17 "2022-11-25T15:29:56Z")

</div>

@couet This output is terribly wrong. The regexp `"Core"` should return nothing (there is no library with the full file name “`Core`”. Only a regexp like `".*Core.*"` should return both libraries.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 25, 2022, 3:52pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/18 "2022-11-25T15:52:20Z")

</div>

@Wile_E_Coyote

That’s exactly the problem the regular expression like `".*Core.*"` does not work as a regular expression as it is a wildcard (cf my previous post) and it returns nothing. (bug ?) … now `gSystem->ListLibraries("Core")` returns nothing either (as before).

But, with the PR, when you put the flag to `kFALSE` then `regexp` is considered as a string and you get the two libraries. The PR does not change anything compared to the current behavior. Seems to me it is buggy but you said we should not change it and you proposed a quite complex workaround.

Now I added the possibility to change the flag. So we can tell the method to not consider `regexp` as a wildcard. The logic is already encoded in `GetLibraries`, this flag only allows us to trigger it. By doing that (see the new help) you can display the two “Core”.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 25, 2022, 4:13pm UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/19 "2022-11-25T16:13:14Z")

</div>

So, maybe stop calling this parameter “regexp”, because it’s misleading. Change its name to “substring”. Then, when the “wildcard = true” (the default now), it will be treated as a ROOT “wildcard expression”, and when “wildcard = false”, it will be treated as an ordinary “substring” (i.e., it’s never a “regular expression”).

I think the core of the problem now is that the [TSystem::GetLibraries](https://root.cern/doc/master/classTSystem.html#ad340f02c58426db03aefc78f73f5cded) method names its parameter “regexp”, but it’s never a “regular expression” (again, it’s either treated as a ROOT “wildcard expression” or as an ordinary “substring”).

Well, maybe the original author of the ROOT “wildcard expression” source code knows how to force it to treat “/” as a regular character.

See my “`ListLibraries.cxx`” macro. It really takes a “regular expression” as a parameter.

---

<div class="post-metadata">

**Author:** ![abelloni](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@abelloni](https://root-forum.cern.ch/u/abelloni)\
**Post date:** [November 28, 2022, 8:26am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/20 "2022-11-28T08:26:37Z")

</div>

Dear all,

let me thank you for your interest in finding a solution! From a user’s standpoint, it would be great if the name of the parameter were updated, to clarify what, by default, the parameter represents, and have the possibility (e.g., via an additional flag, as you both suggested) to pass a real regular expression. That would make the command very powerful and flexible. I believe I did try both `".*"` (reg exp) and `"*"` (a wildcard expression?), and in both cases the command returned 0 libraries found, hence my original message. Thank you very much again for your suggestions, and proposals for a fix! Best,  
Alberto

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 28, 2022, 8:39am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/21 "2022-11-28T08:39:29Z")

</div>

Hi Alberto,  
I will update the PR today.  
Cheers,  
Olivier

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 28, 2022, 8:43am UTC](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/22 "2022-11-28T08:43:00Z")

</div>

Just remember that, also with the new PR, [you will NOT get the “regular expression” functionality](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503/17).

[Next page](https://root-forum.cern.ch/t/regex-usage-in-tsystem-listlibraries/52503.md?page=2)
