I quite often use the regular expression search and replace in SQL Server Management Studio 10.5 editor to clean up auto generated sql before use. The same behaviour described below occurs in Visual Studio 2010 editor as well.
I have the following sql insert statement that I’d like to clean up:
INSERT INTO [lttadev].[dbo].[GameInst]
([GameInstId]
,[GameSetId]
,[UserInfoId]
,[GameLevelId]
,[CreatedOn]
,[CreatedBy]
,[ModifiedOn]
,[ModifiedBy])
VALUES
(<GameInstId, uniqueidentifier,>
,<GameSetId, uniqueidentifier,>
,<UserInfoId, uniqueidentifier,>
,<GameLevelId, uniqueidentifier,>
,<CreatedOn, datetime,>
,<CreatedBy, uniqueidentifier,>
,<ModifiedOn, datetime,>
,<ModifiedBy, uniqueidentifier,>)
To alter the values clause I have the two following regular expressions:
,[^,]*,\>
\<
Both are replaced by an empty string to delete the unwanted text. The first one strips out the comma, type, second comma and final angle bracket. The second one strips out the initial angle bracket. Both work as expected.
However if I join the regexes up into a single expression to speed the text processing, they select different text:
(,[^,]*,\>|\<)
The first expression selects the expected text. However the second expression gets the first angle bracket as well as the preceding comma. Is this a defect in the regular expression engine or am I not understanding something here?
Try wrapping the first alternative in parentheses:
The documentation doesn’t indicate what precedence
|has, and it gives only this example:but elsewhere on that page are various examples where
|is used with parentheses that would have been unnecessary (though harmless) in another regex engine:so I’m guessing that
|might have a fairly high precedence — lower than the trivial within-string concatenation ofspongeormud, but higher than more complex concatenations like in"[^"]*". (Of course, that last expression was written by someone who didn’t notice that.has a special meaning, so it may warrant a grain of salt.)Since I don’t have Visual Studio, I can’t test this out further.