Regulat expression question

  • Thread starter Thread starter mamin
  • Start date Start date
M

mamin

Hi, I have the following string:

"Data source=(local);Initial catalog=GeomelioDaneEwidencyjne;User
ID=aaa;Password=bbbbb;"

I need to get values of User ID from it. (aaa),

how can I do it using regular expressions?
 
Hi, I have the following string:

"Data source=(local);Initial catalog=GeomelioDaneEwidencyjne;User
ID=aaa;Password=bbbbb;"

I need to get values of User ID from it. (aaa),

how can I do it using regular expressions?

Adding the special case that the user id may be the last parameter, not
followed by a semicolon (which is not appearent from your example), this
pattern would work:

"(?:^|;)User ID=([^;]+)(?:;|$)"
 
* Göran Andersson wrote, On 2-5-2007 15:20:
Hi, I have the following string:

"Data source=(local);Initial catalog=GeomelioDaneEwidencyjne;User
ID=aaa;Password=bbbbb;"

I need to get values of User ID from it. (aaa),

how can I do it using regular expressions?

Adding the special case that the user id may be the last parameter, not
followed by a semicolon (which is not appearent from your example), this
pattern would work:

"(?:^|;)User ID=([^;]+)(?:;|$)"

(?<=(?:;|^)\s*User ID\s*=\s*)[^;]+

Should result in only the text following User ID as match. Which is
probably easier to use.

Jesse
 
(?<=User\s+ID\s*=\s*)[^;]+

It's not necessary to test for any previous text. Also, this version
accounts for possible line breaks in the Connection String.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Jesse Houwing said:
* Göran Andersson wrote, On 2-5-2007 15:20:
Hi, I have the following string:

"Data source=(local);Initial catalog=GeomelioDaneEwidencyjne;User
ID=aaa;Password=bbbbb;"

I need to get values of User ID from it. (aaa),

how can I do it using regular expressions?

Adding the special case that the user id may be the last parameter, not
followed by a semicolon (which is not appearent from your example), this
pattern would work:

"(?:^|;)User ID=([^;]+)(?:;|$)"

(?<=(?:;|^)\s*User ID\s*=\s*)[^;]+

Should result in only the text following User ID as match. Which is
probably easier to use.

Jesse
 
Kevin said:
(?<=User\s+ID\s*=\s*)[^;]+

It's not necessary to test for any previous text. Also, this version
accounts for possible line breaks in the Connection String.

Not testing for any previous text assumes that there is no other
property that ends with "user id". There probably isn't, but I would
still suggest checking for beginning of text or ; before it. If nothing
else, so that the code can be reused to check for any property name.
 
Yes, but note that the regular expression uses a positive look-behind
(assertion) with the entire string "User ID = " in it, and only terminates
with a semicolon. The syntax of the Connection String would not be legal
(and it would not work) if this were anything other than the "User ID"
portion of the Connection String. So, I eliminated the "redundant" check. I
should mention that I am intimately familiar with Connection String syntax.
If I was unsure, I would have gone with your technique.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Göran Andersson said:
Kevin said:
(?<=User\s+ID\s*=\s*)[^;]+

It's not necessary to test for any previous text. Also, this version
accounts for possible line breaks in the Connection String.

Not testing for any previous text assumes that there is no other property
that ends with "user id". There probably isn't, but I would still suggest
checking for beginning of text or ; before it. If nothing else, so that
the code can be reused to check for any property name.
 
Kevin said:
Yes, but note that the regular expression uses a positive look-behind
(assertion) with the entire string "User ID = " in it, and only terminates
with a semicolon. The syntax of the Connection String would not be legal
(and it would not work) if this were anything other than the "User ID"
portion of the Connection String. So, I eliminated the "redundant" check. I
should mention that I am intimately familiar with Connection String syntax.
If I was unsure, I would have gone with your technique.

Assuming that it actually is a connection string... It probably is, but
there is nothing in the OP that says that it is. :)
 
* Kevin Spencer wrote, On 3-5-2007 14:08:
(?<=User\s+ID\s*=\s*)[^;]+

It's not necessary to test for any previous text. Also, this version
accounts for possible line breaks in the Connection String.

You will want to test against some etch in front of User ID, when for
example the password of the database name contains this value. I know
it's a corner case, but I'd rather be safe than sorry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top