最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PowerShell Select-String fails with "Illegalat end of pattern" when searching for directory - Sta

programmeradmin3浏览0评论

I'm using PowerShell to find ignored files in my Git repository that belong to a specific directory, company_logos. I'm running the following command in my project folder:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String "company_logos" However, I get this error:

vbnet Copy Edit Select-String : The string \company_logos\ is not a valid regular expression: parsing "\company_logos" - Illegal \ at end of pattern.
At line:1 char:54

  • ... ignored --others --exclude-standard | Select-String "company_logos"
  •                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Select-String], ArgumentException
    • FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand It seems like PowerShell is interpreting company_logos as a regex pattern, and the backslashes () in the path are causing an issue.

What I’ve tried: Escaping the backslashes:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String "company_logos" But I still get the same issue.

Using single quotes:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String 'company_logos' This also didn't work.

Using -SimpleMatch:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String -SimpleMatch "company_logos" This worked, but I want to understand why the original command failed and how to properly escape it.

Question: Why does Select-String throw the "Illegal \ at end of pattern" error in my original command? What is the correct way to search for a directory name like company_logos in this context? Any insights would be appreciated!

I'm using PowerShell to find ignored files in my Git repository that belong to a specific directory, company_logos. I'm running the following command in my project folder:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String "company_logos" However, I get this error:

vbnet Copy Edit Select-String : The string \company_logos\ is not a valid regular expression: parsing "\company_logos" - Illegal \ at end of pattern.
At line:1 char:54

  • ... ignored --others --exclude-standard | Select-String "company_logos"
  •                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Select-String], ArgumentException
    • FullyQualifiedErrorId : InvalidRegex,Microsoft.PowerShell.Commands.SelectStringCommand It seems like PowerShell is interpreting company_logos as a regex pattern, and the backslashes () in the path are causing an issue.

What I’ve tried: Escaping the backslashes:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String "company_logos" But I still get the same issue.

Using single quotes:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String 'company_logos' This also didn't work.

Using -SimpleMatch:

powershell Copy Edit git ls-files --ignored --others --exclude-standard | Select-String -SimpleMatch "company_logos" This worked, but I want to understand why the original command failed and how to properly escape it.

Question: Why does Select-String throw the "Illegal \ at end of pattern" error in my original command? What is the correct way to search for a directory name like company_logos in this context? Any insights would be appreciated!

Share Improve this question asked Mar 19 at 4:46 CoupynCoupyn 12 bronze badges 3
  • Can you please first of all apply proper code formatting, because right now, about half of the above isn't displaying correctly. stackoverflow/help/formatting – C3roe Commented Mar 19 at 7:08
  • I'm guessing you should have a look at learn.microsoft/en-us/powershell/module/… – C3roe Commented Mar 19 at 7:09
  • This question could use some clarification and code cleanup. Every section listed "powershell Copy Edit", and then the command used, those need to be placed within code blocks, and Copy Edit dont need to be there. – NodeDad Commented Mar 22 at 16:39
Add a comment  | 

1 Answer 1

Reset to default 0

If you change directory to company_logos in the local repo before running the git-ls-files command, you will get the relative path to ignored files in that directory without needing to pipe to select-string. If you need the full path add --full-name https://git-scm/docs/git-ls-files.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论