3
votes

Given the below sample, how would I match each hyphen found between the words "CAST" and "DATETIME"? (CAST(N'2013-11-26 10:52:47.957' AS DateTime))

The pattern occurs multiple times per row. There could be hyphens anywhere else in the string that must not be matched.

INSERT [dbo].[tbl_Content] ([Template], [CreatedDate], [Url], [PublishedDate]) VALUES (N’gallery-item.aspx', CAST(N'2013-11-26 10:52:47.957' AS DateTime), N'some-url', CAST(N'2013-11-26 00:00:00.000' AS DateTime))
INSERT [dbo].[tbl_Content] ([Template], [CreatedDate], [Url], [PublishedDate]) VALUES (N’another-item.aspx', CAST(N'2013-11-26 10:52:47.957' AS DateTime), N'some-other-url', CAST(N'2013-11-26 00:00:00.000' AS DateTime))

CAST(.*)DateTime selects all characters between the first occurrence of "CAST" and the last occurrence of "DATETIME" on each row. there could be other hyphens in this selection that shouldn't be matched.

- will match any hyphen in the document.

I guess I need to combine these two patterns somehow but my regex knowledge is non-existent. CAST(-)DateTime doesn't work.

What is the correct way to do this?

If platform is important: this will be used to do find-replace in Visual Studio Code. If this is not possible, I'm absolutely open to using another text/code editor.

1
That depends on what tool/regex library you are using.Wiktor Stribiżew
Hi @WiktorStribiżew, I'm wanting to use this in a find-replace in Visual Studio Code. I'm afraid I have no idea which libray they use in the backgroundTurnip
Sorry, but it is impossible, since VSC uses ECMAScript 5 regex.Wiktor Stribiżew
“CAST(.*)DateTime selects all characters between the first occurrence of "CAST" and the last occurrence of "DATETIME" on each row” – that’s because regex are “greedy.” So you need to switch to un-greedy mode for the .* part. (I don’t know what “flavor” of regex Visual Studio is using, so you’ll need to go look up yourself how to do that in there.)CBroe
what if hyphen selected around digits..?means like : regex101.com/r/dB2fB3/1Shekhar Khairnar

1 Answers

1
votes

If you can use Notepad++, you may use a regex based on a \G operator that helps find consecutive matches after an initial match.

Use

(?:\bCAST\b|(?!^)\G)(?:(?!\b(?:DATETIME|CAST)\b)[^-])*\K-

and replace with any symbol(s) you want (remember that parentheses must be escaped in NPP replacement patterns). Below, I replaced the - with §.

Details:

  • (?:\bCAST\b|(?!^)\G) - a whole word CAST or the end position of the previous match
  • (?:(?!\b(?:DATETIME|CAST)\b)[^-])* - a tempered greedy token that matches any char but - (see [^-]) that does not start a sequence of whole words DATETIME or CAST (see the negative lookahead (?!\b(?:DATETIME|CAST)\b))
  • \K - a match reset opeator: all text matched so far is discarded
  • - - the hyphen we want to match

enter image description here