-2
echo a.txt| FINDSTR /R ".+\.txt$"

not work and not even:

echo a.txt| FINDSTR /R .+\.txt$

Why?

Edit:

Now works!

echo a.txt| FINDSTR /R ..*\.txt$
Mario Palumbo
  • 693
  • 8
  • 32

1 Answers1

4

a) There is no + in the regex of Findstr. Use * instead. ..* should be the correct replacement for .+.

b) There can be invisible character before the end of the line, e.g. caused by echo if there's a trailing space before |. Add another . before the line end to cover that.

C:\> echo a.txt | findstr /r ".*\.txt.$"
a.txt

It's also possible without the quotation marks.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • 3
    @ThomasWeller The invisible character is the space between `a.txt` and `|` which is also output by `echo` and therefore redirected to handle __STDIN__ of command `findstr`. Everything is as expected on using `echo a.txt| findstr /R ".*\.txt$"`. See for example my answer on [Why does ECHO command print some extra trailing space into the file?](https://stackoverflow.com/a/46972524/3074564) for details. – Mofi Dec 06 '18 at 06:55
  • 3
    Changing `+` to `*` changes the behaviour; rather replace `.+` by `..*` to achieve the same result... – aschipfl Dec 06 '18 at 10:02
  • @aschipfl: great suggestion. I added this into the answer – Thomas Weller Dec 06 '18 at 13:02