# Strange behavior of itk::RegularExpressionSeriesFileNames

**URL:** <https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319>\
**Category:** Engineering\
**Created:** [November 19, 2024, 1:52pm UTC](https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319 "2024-11-19T13:52:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![acoussat](https://discourse.itk.org/user_avatar/discourse.itk.org/acoussat/32/699_2.png) [@acoussat](https://discourse.itk.org/u/acoussat)\
**Post date:** [November 19, 2024, 1:52pm UTC](https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319/1 "2024-11-19T13:52:37Z")

</div>

Hello,

I came across a strange behavior of `itk::RegularExpressionSeriesFileNames`, and either I miss something, or there is a bug somewhere.

I have the following directory structure:

```auto
projections
├── projections0000.mhd
├── projections0000.raw
├── projections0001.mhd
├── projections0001.raw
├── projections0002.mhd
├── projections0002.raw
├── ...
├── projections0359.mhd
└── projections0359.raw
projections_cpp
├── projections0000.mhd
├── projections0000.raw
├── projections0001.mhd
├── projections0001.raw
├── projections0002.mhd
├── projections0002.raw
├── ...
├── projections0359.mhd
└── projections0359.raw

```

Then, I run the following code:

```cpp
#include <itkRegularExpressionSeriesFileNames.h>

int main(int argc, char * argv[])
{
  itk::RegularExpressionSeriesFileNames::Pointer names = itk::RegularExpressionSeriesFileNames::New();
  names->SetDirectory("projections");
  names->SetNumericSort(false);
  names->SetRegularExpression(".mhd");
  names->SetSubMatch(0);

  for(auto filename : names->GetFileNames()){
    std::cout << filename << std::endl;
  }

  std::cout << "-------" << std::endl;

  itk::RegularExpressionSeriesFileNames::Pointer names2 = itk::RegularExpressionSeriesFileNames::New();
  names2->SetDirectory("projections_cpp");
  names2->SetNumericSort(false);
  names2->SetRegularExpression(".mhd");
  names2->SetSubMatch(0);

  for(auto filename : names2->GetFileNames()){
    std::cout << filename << std::endl;
  }

  return EXIT_SUCCESS;
}

```

I expect to get the alphabetical order of the two `itk::RegularExpressionSeriesFileNames` to be the same. However, I get some strange differences, as illustrated below:

 ![image](https://discourse.itk.org/uploads/default/original/2X/2/25f47fe60e5cc6b781b1212fa4869a6563b62476.png)

I understand that in that specific case, one should rather use a numeric sort. But still, shouldn’t the order be exactly the same in the two cases?

I believe that the problem, if there is one, lies somewhere around those lines of code: [ITK/Modules/IO/ImageBase/src/itkRegularExpressionSeriesFileNames.cxx at 9dc398562a0ecfa792efbb6744e166f0c0e0d069 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/blob/9dc398562a0ecfa792efbb6744e166f0c0e0d069/Modules/IO/ImageBase/src/itkRegularExpressionSeriesFileNames.cxx#L35)

Let me know what you think. Thanks!

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [November 19, 2024, 2:16pm UTC](https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319/2 "2024-11-19T14:16:49Z")

</div>

Maybe the order is dependent on the file system? You did `SetNumericSort(false)`.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [November 19, 2024, 2:28pm UTC](https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319/3 "2024-11-19T14:28:15Z")

</div>

Your regular expression it “.mhd”. The ‘.’ matches any character followed by “mhd”. The will be the same “match” for each file and work results with the default ordering from the file system.

Try “.\*mhd” for the regular expression.

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [November 19, 2024, 4:26pm UTC](https://discourse.itk.org/t/strange-behavior-of-itk-regularexpressionseriesfilenames/7319/4 "2024-11-19T16:26:44Z")

</div>

I think you don’t have a submatch which is why alphabetic ordering does not work. I would try “(.\*).mhd” where the parentheses indicate the submatch location.
