Conditional Replace with Visual Studio

拟墨画扇 提交于 2021-02-16 17:58:09

问题


In Visual Studio, I need to substitute a word with another, preserving the first character case. For example I need to subsitute "Bob" with "James" and "bob" with "james" at once, and I must avoid to replace partial matches like "ob" with "james" or "James".

This can be done e.g. in Notepad++ with find:"((b)|(B))ob", replace: "(?2j:?3J)ames"; unfortunately this does not work in Visual Studio (I'm using 2015). Is it possible to do this in Visual Studio? Thanks.


回答1:


It is not possible with Visual Studio regex replace feature. Use Notepad++ with your current approach, or use separate regex replacements:

Search: \bBob\b
Replace: James

and then

Search: \bbob\b
Replace: james

Note that \b is a word boundary. If you need to replace all substrings regardless of whether Bob or bob are whole words, remove the \b from the patterns.



来源:https://stackoverflow.com/questions/41298753/conditional-replace-with-visual-studio

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