Powershell - How to search for Whole Words (or) Exact Matches

谁都会走 提交于 2021-02-08 04:00:35

问题


this is my requirement.

I need to find if a certain CSS classes is referenced in my Solution which has over 120 aspx pages.

This is the command that we wrote

gci . -include *.aspx -recurse | select-string -pattern ".rtop" -caseSensitive >> D:\CSSResult.txt

However, this matches even words with rtop in middle. That is, if I have a variable named as dirtopy, it comes in the result list. I do not want that.

I want it in the result only if an exact match of .rtop is found.

How do I do it?

Any references or examples would be more than sufficient.

Thanks!


回答1:


Try select-string -pattern "\.rtop\b".

You need to escape the dot, or it will match any character; and the \b word boundary anchor ensures that the word does not continue after rtop.




回答2:


You can use the -simplematch option of select-string to do a literal match instead of regex.



来源:https://stackoverflow.com/questions/5168616/powershell-how-to-search-for-whole-words-or-exact-matches

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!