1

I'm implementing my own DBCommand like wrapper for our proprietary database through which we can only access via API and by using strings. Boooo

I want to be able to find via Regular Expression my normal parameters and not select the @@IDENTITY field (or anything else like that). I'm then going to replace the parameter name with the real value of that parameter NULL or 'Some Value' etc.

Currently I have

@\w{1,}

Given a string

INSERT INTO MYTABLE(VALUE1, VALUE2) VALUES (@MyValue1, @MyValue2); SELECT @@IDENTITY

I'm matching

@MyValue1
@MyValue2
@IDENTITY

I also tried [^@]@\w{1,} but it matches (@MyValue1 and _@MyValue2 (underscore representing space)

How can I not match @@Identity?

3 Answers 3

3

You can add paretheses, marking the subexpression whose matching string you actually want to use: [^@](@\w{1,}), then use Match.Groups(1) to retrieve that part only.

Another variant is a negative look-behind assertion: (?<!@)@\w{1,}, with an additional bonus of matching @name in the very beginning of the string (well, it probably won't happen).

Update: and it won't help you for '@quoted @string @literals', which are legitimate SQL, but not parameters

Sign up to request clarification or add additional context in comments.

Comments

2

Try this,i assume this what you want.

(?<!@)@\w+

Comments

2

You could use a lookbehind to ensure it doesn't start with 2 @:

(?<!@)@\w{1,}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.