Click here to Skip to main content
15,903,203 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: sub string using user input Pin
Gerry Schmitz31-Oct-21 7:38
mveGerry Schmitz31-Oct-21 7:38 
AnswerRe: sub string using user input Pin
Johnny J.30-Oct-21 22:59
professionalJohnny J.30-Oct-21 22:59 
GeneralRe: sub string using user input Pin
OriginalGriff30-Oct-21 23:31
mveOriginalGriff30-Oct-21 23:31 
GeneralRe: sub string using user input Pin
Mike Hankey31-Oct-21 8:03
mveMike Hankey31-Oct-21 8:03 
AnswerRe: sub string using user input Pin
Eddy Vluggen31-Oct-21 2:50
professionalEddy Vluggen31-Oct-21 2:50 
GeneralEureka! regex matching with stored procedures PinPopular
honey the codewitch30-Oct-21 8:57
mvahoney the codewitch30-Oct-21 8:57 
GeneralRe: Eureka! regex matching with stored procedures Pin
jsc4230-Oct-21 10:29
professionaljsc4230-Oct-21 10:29 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 10:46
mvahoney the codewitch30-Oct-21 10:46 
Keep in mind this was generated by a tool, so the table names would be different for a different input specification file. The trick with this routine is filling the state tables properly

The reason it's so nasty is fetching the next UTF32 codepoint in SQL is a pain in my backside

SQL
CREATE PROCEDURE [dbo].[Example_Match] @value NVARCHAR(MAX), @symbolId INT
AS
BEGIN
	DECLARE @valueEnd INT = DATALENGTH(@value)/2+1
	DECLARE @index INT = 1
	DECLARE @ch BIGINT
	DECLARE @ch1 NCHAR
	DECLARE @ch2 NCHAR
	DECLARE @tch BIGINT
	DECLARE @state INT = 0
	DECLARE @toState INT = -1
	DECLARE @accept INT = -1
	DECLARE @position BIGINT = 0
	DECLARE @capture NVARCHAR(MAX)
	DECLARE @blockEndId INT
	DECLARE @result INT = 0
	DECLARE @len INT = 0
	DECLARE @done INT = 0
	CREATE TABLE #Results (
    [Position]  BIGINT NOT NULL,
    [Value] NVARCHAR(MAX) NOT NULL,
    [Length] INT NOT NULL
	)
	IF @index >= @valueEnd
	BEGIN 
		SET @ch = -1
	END
	ELSE
	BEGIN
		SET @ch1 = SUBSTRING(@value,@index,1)
		SET @ch = UNICODE(@ch1)
		SET @tch = @ch - 0xd800
		IF @tch < 0 SET @tch = @tch + 2147483648
		IF @tch < 2048
		BEGIN
			SET @ch = @ch * 1024
			SET @index = @index + 1
			IF @index >= @valueEnd RETURN -1
			SET @ch2 = SUBSTRING(@value,@index,1);
			SET @ch = @ch + UNICODE(@ch2) - 0x35fdc00
		END
	END
	WHILE @ch <> -1
	BEGIN
		SET @capture = N''
		SET @position = @index - 1
		SET @done = 0
		WHILE @done = 0
		BEGIN 
			SET @done = 1
			SET @toState = -1
			SELECT @toState = [dbo].[ExampleStateTransition].[ToStateId] FROM [dbo].[ExampleState] INNER JOIN [dbo].[ExampleStateTransition] ON [dbo].[ExampleState].[StateId]=[dbo].[ExampleStateTransition].[StateId] AND [dbo].[ExampleState].[SymbolId]=[dbo].[ExampleStateTransition].[SymbolId] AND [dbo].[ExampleStateTransition].[BlockEndId]=-1 WHERE [dbo].[ExampleState].[SymbolId]=@symbolId AND [dbo].[ExampleState].[StateId]=@state AND [dbo].[ExampleState].[BlockEndId] = -1 AND @ch BETWEEN [dbo].[ExampleStateTransition].[Min] AND [dbo].[ExampleStateTransition].[Max] 
			IF @toState <> -1
			BEGIN
				SET @done = 0
				SET @state = @toState;
				SET @capture = @capture + @ch1
				IF @tch < 2048 SET @capture = @capture + @ch2
				SET @index = @index + 1
				IF @index >= @valueEnd
				BEGIN
					SET @ch = -1
					SET @done = 1
				END
				ELSE
				BEGIN
					SET @ch1 = SUBSTRING(@value,@index,1)
					SET @ch = UNICODE(@ch1)
					SET @tch = @ch - 0xd800
					IF @tch < 0 SET @tch = @tch + 2147483648
					IF @tch < 2048
					BEGIN
						SET @ch = @ch * 1024
						SET @index = @index + 1
						IF @index >= @valueEnd RETURN -1
						SET @ch2 = SUBSTRING(@value,@index,1);
						SET @ch = @ch + UNICODE(@ch2) - 0x35fdc00
					END
				END
			END
		END
		SET @accept = -1
		SELECT @accept = [dbo].[ExampleState].[SymbolId] FROM [dbo].[ExampleState] WHERE [dbo].[ExampleState].[SymbolId] = @symbolId AND [dbo].[ExampleState].[StateId] = @state AND [dbo].[ExampleState].[BlockEndId] = -1 AND [dbo].[ExampleState].[Accepts]=1
		IF @accept <> -1 
		BEGIN
			SELECT TOP 1 @blockEndId = [dbo].[ExampleState].[BlockEndId] FROM [dbo].[ExampleState] WHERE [dbo].[ExampleState].[SymbolId]=@symbolId AND [dbo].[ExampleState].[BlockEndId] <> -1
			IF @blockEndId <> -1 
			BEGIN
				SET @result = 0
				SET @state = 0
				WHILE @ch <> -1
				BEGIN
					SET @done = 0
					WHILE @done = 0
					BEGIN
						SET @done = 1
						SET @toState = -1
						SELECT @toState = [dbo].[ExampleStateTransition].[ToStateId] FROM [dbo].[ExampleState] INNER JOIN [dbo].[ExampleStateTransition] ON [dbo].[ExampleState].[StateId]=[dbo].[ExampleStateTransition].[StateId] AND [dbo].[ExampleState].[SymbolId]=[dbo].[ExampleStateTransition].[SymbolId] AND [dbo].[ExampleStateTransition].[BlockEndId]=@blockEndId WHERE [dbo].[ExampleState].[SymbolId]=@symbolId AND [dbo].[ExampleState].[StateId]=@state AND [dbo].[ExampleState].[BlockEndId] = @blockEndId AND @ch BETWEEN [dbo].[ExampleStateTransition].[Min] AND [dbo].[ExampleStateTransition].[Max] 
						IF @toState <> -1
						BEGIN
							SET @done = 0
							SET @state = @toState
							SET @capture = @capture + @ch1
							IF @tch < 2048 SET @capture = @capture + @ch2
							SET @index = @index + 1
							IF @index >= @valueEnd 
							BEGIN
								SET @ch = -1
								SET @done = 1
							END
							ELSE
							BEGIN
								SET @ch1 = SUBSTRING(@value,@index,1)
								SET @ch = UNICODE(@ch1)
								SET @tch = @ch - 0xd800
								IF @tch < 0 SET @tch = @tch + 2147483648
								IF @tch < 2048
								BEGIN
									SET @ch = @ch * 1024
									SET @index = @index + 1
									IF @index >= @valueEnd RETURN -1
									SET @ch2 = SUBSTRING(@value,@index,1);
									SET @ch = @ch + UNICODE(@ch2) - 0x35fdc00
								END
							END
						END	
					END -- WHILE @done = 0
					SET @accept = -1
					SELECT @accept = [dbo].[ExampleState].[SymbolId] FROM [dbo].[ExampleState] WHERE [dbo].[ExampleState].[SymbolId] = @symbolId AND [dbo].[ExampleState].[StateId] = @state AND [dbo].[ExampleState].[BlockEndId] = @blockEndId AND [dbo].[ExampleState].[Accepts]=1
					IF @accept <> -1 
					BEGIN
						INSERT INTO #Results SELECT @position AS [Position], @capture AS [Value], DATALENGTH(@capture)/2 as [Length]
						SET @state = 0
                        BREAK
					END
					ELSE -- IF @accept <> -1 
					BEGIN
						SET @capture = @capture + @ch1
						IF @tch < 2048 SET @capture = @capture + @ch2
						SET @index = @index + 1
						IF @index >= @valueEnd 
						BEGIN
							SET @ch = -1
							SET @done = 1
						END
						ELSE -- IF @index >= @valueEnd 
						BEGIN
							SET @ch1 = SUBSTRING(@value,@index,1)
							SET @ch = UNICODE(@ch1)
							SET @tch = @ch - 0xd800
							IF @tch < 0 SET @tch = @tch + 2147483648
							IF @tch < 2048
							BEGIN
								SET @ch = @ch * 1024
								SET @index = @index + 1
								IF @index >= @valueEnd RETURN -1
								SET @ch2 = SUBSTRING(@value,@index,1);
								SET @ch = @ch + UNICODE(@ch2) - 0x35fdc00
							END
						END -- IF @index >= @valueEnd 
					END -- IF @accept <> -1 
					SET @state = 0
				END -- WHILE ch<>-1
				SET @state = 0
				CONTINUE
			END
			ELSE
			BEGIN
			SET @len = DATALENGTH(@capture)/2
			IF(@len>0) INSERT INTO #Results SELECT @position AS [Position], @capture AS [Value], @len as [Length]
			END
		END -- IF @accept <> -1
		SET @index = @index + 1
		IF @index >= @valueEnd 
		BEGIN
			SET @ch = -1
			SET @done = 1
		END
		ELSE
		BEGIN
			SET @ch1 = SUBSTRING(@value,@index,1)
			SET @ch = UNICODE(@ch1)
			SET @tch = @ch - 0xd800
			IF @tch < 0 SET @tch = @tch + 2147483648
			IF @tch < 2048
			BEGIN
				SET @ch = @ch * 1024
				SET @index = @index + 1
				IF @index >= @valueEnd RETURN -1
				SET @ch2 = SUBSTRING(@value,@index,1);
				SET @ch = @ch + UNICODE(@ch2) - 0x35fdc00
			END
		END
	END
	SELECT * FROM #Results
	DROP TABLE #Results
END

Real programmers use butterflies

GeneralRe: Eureka! regex matching with stored procedures Pin
Nelek30-Oct-21 12:46
protectorNelek30-Oct-21 12:46 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 13:37
mvahoney the codewitch30-Oct-21 13:37 
GeneralRe: Eureka! regex matching with stored procedures Pin
Nelek30-Oct-21 22:05
protectorNelek30-Oct-21 22:05 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 22:10
mvahoney the codewitch30-Oct-21 22:10 
GeneralRe: Eureka! regex matching with stored procedures Pin
Chris Maunder31-Oct-21 6:01
cofounderChris Maunder31-Oct-21 6:01 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch31-Oct-21 6:42
mvahoney the codewitch31-Oct-21 6:42 
GeneralRe: Eureka! regex matching with stored procedures Pin
Chris Maunder31-Oct-21 6:53
cofounderChris Maunder31-Oct-21 6:53 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch31-Oct-21 7:09
mvahoney the codewitch31-Oct-21 7:09 
GeneralRe: Eureka! regex matching with stored procedures Pin
Chris Maunder1-Nov-21 3:16
cofounderChris Maunder1-Nov-21 3:16 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch1-Nov-21 3:35
mvahoney the codewitch1-Nov-21 3:35 
GeneralRe: Eureka! regex matching with stored procedures Pin
Marc Clifton30-Oct-21 11:43
mvaMarc Clifton30-Oct-21 11:43 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 12:00
mvahoney the codewitch30-Oct-21 12:00 
GeneralRe: Eureka! regex matching with stored procedures Pin
Marc Clifton30-Oct-21 14:37
mvaMarc Clifton30-Oct-21 14:37 
GeneralRe: Eureka! regex matching with stored procedures Pin
PIEBALDconsult30-Oct-21 14:12
mvePIEBALDconsult30-Oct-21 14:12 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 14:23
mvahoney the codewitch30-Oct-21 14:23 
GeneralRe: Eureka! regex matching with stored procedures Pin
PIEBALDconsult30-Oct-21 15:26
mvePIEBALDconsult30-Oct-21 15:26 
GeneralRe: Eureka! regex matching with stored procedures Pin
honey the codewitch30-Oct-21 15:37
mvahoney the codewitch30-Oct-21 15:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.