How do I find {min,max} repeats with regular expression patterns in Visual Studio or SSMS “Find and Replace”?

时光总嘲笑我的痴心妄想 提交于 2019-12-22 05:38:07

问题


I knew that we have something like this in the regular expression syntax world.

*The syntax is {min,max}, where min is a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches.

So {0,} is the same as *, and {1,} is the same as +*.

http://www.regular-expressions.info/repeat.html


But how can I use it in SQL Server Management Studio or Visual Studio's "Find and Replace" window. I only find related Microsoft syntax in MSDN. Like:

[0-9]^4 matches any 4-digit sequence.


回答1:


The Visual Studio regex implementation (in versions up until Visual Studio 2010) is a fairly nonstandard one to say the least, and it doesn't have this feature. You can only spell it out:

* or @: Match zero or more of the preceding expression

+ or #: Match one or more of the preceding expression

^n: Match exactly n repetitions of the preceding expression

So for A{2,4} you'd have to use A^4|A^3|A^2 (see polygenelubricant's comment for an explanation why you need to do it in descending order).

More recent versions of Visual Studio support the entire set of .NET regexes.



来源:https://stackoverflow.com/questions/3350828/how-do-i-find-min-max-repeats-with-regular-expression-patterns-in-visual-studi

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