1

I have this code

Regex.Match(contents, @"Security=(?<Security>\D+)").Groups["Security"].Value;  

this makes the following:

Security=SSPI;Database=Datab_ob

How do I make the Regex cut off at ; so i would only get Security=SSPI

1
  • If you put four spaces before each line of code it gets formatted as code. This means the HTML sanitizer won't eat tag-like stuff, like the <Security> group name. Commented Dec 7, 2009 at 15:52

3 Answers 3

3
Regex.Match(contents, "Security=(?<Security>[^;]+)").Groups["Security"].Value
Sign up to request clarification or add additional context in comments.

1 Comment

+1; Anytime you have a text delimiter that's defined by some external constraint (like the use of ';' in the connection string parsing logic in .NET) using that as a negative character class makes for a simple and reliable regex.
0

How about this?

 "Security=(?<Security>\D+?);"

Comments

0

you could to use a positive lookahead to look for the ; after your string, but not to match it. Using this:

Security=(?<Security>\w+(?=;))

as the regex pattern will get any words after the '=' and before the ';' into the Security named group

1 Comment

That's if you want there to be a ; after the Security line. It could also be at the end of the connection string.

Your Answer

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