Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 816
Iman Avatar asked Jul 28 '10 07:07

Iman


People also ask

How do I use regular expressions in Visual Studio search?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

What output method should be used when using RegEx to output each separate instance of the expression found in the string?

RegEx Tokenize This output method is similar to the Text to Columns tool but gives you much more flexibility: Alteryx searches the string input for each instances of your regex, placing each into a separate column or row.

What is RegEx in Visual Studio?

Regular expressions, also referred to as “regex” in the developer community, is an extremely powerful tool used in pattern matching and substitution.

What type of RegEx does Alteryx use?

Regular expressions, most commonly referred to as RegEx (pronounced: Rej-Ex), are a sequence of characters that allows the user to create patterns that help match, locate, and manage any string data. In Alteryx, you can use the RegEx tool to replace, tokenize, parse, and match string values.


1 Answers

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.

like image 150
Tim Pietzcker Avatar answered Nov 15 '22 18:11

Tim Pietzcker