Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Regex '?'

Tags:

MSVS: Where's the regex ?

I have code that I'm trying to match with a regular expression in MSVS 2008, but I can't figure out the regex for it. Take the classic example:

colou?r

...which is a regular expression that matches color or colour. This matches neither in MSVS. Referring to the help file, I cannot find ?.

This wouldn't be a big deal - it can be emulated with alternation:

colo(u|)r

However, I get "Grouped expression is missing ')'."... which it's... not. Oddly, MSVS has these alternate groups (I'm not really sure why...) with curly braces:

colo{u|}r

Which gives me the altogether different error of "Syntax error in pattern."... which, I don't see one. Basically, how do I do a ?? My actual input is not as simple as colour/color, otherwise I'd just fake it with (color|colour). I suppose could fake it, but it's an obtuse way to go about it.


Let's try alternation then...

Ok, I still can't do it, even with alternation. I have the following two regexes:

^[A-Z]+\t[0-9]+\t[^\t]+

^[A-Z]+\t[0-9]+\t[^\t]+\t[^\t]+

Those two match two sets of lines match, individually, my text. (The first one matches part of the lines that match the second one.)

My input is lines of currency information:

BZD 084 Belize dollar
CAD 124 Canadian dollar
CDF 976 Franc Congolais
CHE 947 WIR euro    (complementary currency)
CHF 756 Swiss franc
CHW 948 WIR franc   (complementary currency)
CLF 990 Unidad de Fomento   (funds code)

(There are tabs, for example, between WIR euro and (complementary currency), but they're not always there.)

Logically, it should follow that to combine

^[A-Z]+\t[0-9]+\t[^\t]+

^[A-Z]+\t[0-9]+\t[^\t]+\t[^\t]+

..you get... ^[A-Z]+\t[0-9]+\t([^\t]+|[^\t]+\t[^\t]+) ...which somehow appears to be equivalent to the second expression in the first set.

like image 376
Thanatos Avatar asked Jun 26 '09 19:06

Thanatos


People also ask

What is '?' In regex?

It means "Match zero or one of the group preceding this question mark." It can also be interpreted as the part preceding the question mark is optional. In above example '?' indicates that the two digits preceding it are optional. They may not occur or occur at the most once.

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.

How do I match a group in regex?

Regular expressions allow us to not just match text but also to extract information for further processing. This is done by defining groups of characters and capturing them using the special parentheses ( and ) metacharacters. Any subpattern inside a pair of parentheses will be captured as a group.

What is regex C#?

In C#, Regular Expression is a pattern which is used to parse and check whether the given input text is matching with the given pattern or not. In C#, Regular Expressions are generally termed as C# Regex. The . Net Framework provides a regular expression engine that allows the pattern matching.


1 Answers

Our very own Jeff Atwood wrote about this a while back. Basically, Visual Studio's regex implementation is pretty nonstandard and there's no straightforward way to do what's usually done with '?'. You'll have to use your {colour|color} expression.

like image 197
Welbog Avatar answered Oct 11 '22 10:10

Welbog