views:

37

answers:

1

I knew that we have something like this in 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 "Find and Replace" Window. I only find related Microsoft syntax in MSDN. Like:

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

+2  A: 

The Visual Studio regex implementation 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).

Tim Pietzcker
`A^2|A^3|A^4` is reluctant, `A^4|A^3|A^2` for greedy version.
polygenelubricants
@polygenelubricants: Interesting, thanks for the info. I'm not at home right now and couldn't check this yet. What I was wondering: The documentation doesn't mention a "zero or one" quantifier (`?`) - doesn't it exist?
Tim Pietzcker
@Tim (+1): I know nothing about Visual Studio regex implementation, but (assuming this is where you're going), `A{2,4}` is equivalent to `AAA?A?`. There may be issues with catastrophic backtracking, though. If at all possible, the `?` should be atomic/possessive. As for the greediness, it's because `{2,4}` will first try to take 4 if possible, then if not 3, then if not 2, in that order. Hence `A^4|A^3|A^2`. If it was `{2,4}?` then it's equivalent to `A^2|A^3|A^4`. The difference shows when there are exactly 3 A. The greedy will match all 3, the reluctant will match only 2 leaving 1 unmatched.
polygenelubricants