Help with Regular expression

  • Thread starter Thread starter M. D'Costa
  • Start date Start date
M

M. D'Costa

Hello,

I am trying to write a regular expression to parse the string:

Database=TestDB;Data Source=server;User Id=guest;Password=;

Am doing this to extract the values after the equals sign( =) and before the
semicolon(;)
and I want the values stored in variables that are before the equals
sign.....

I have been semi-successful. Help will be appreciated.

Thanks,
Marise
 
[VB.Net]
Imports System.Text.RegularExpressions

Dim strPattern As String = "(?<name>.+)\=(?<value>.+)\;"
Dim regExp As New RegEx(strPattern, RegExOptions.ExplicitCapture)

Sometimes the greatest solutions come from the simplest logic.
Being told "No" is merely the incentive to do it anyway.
 
M. D'Costa said:
Hello,

I am trying to write a regular expression to parse the string:

Database=TestDB;Data Source=server;User Id=guest;Password=;

Am doing this to extract the values after the equals sign( =) and before
the
semicolon(;)
and I want the values stored in variables that are before the equals
sign.....

I have been semi-successful. Help will be appreciated.

What have you tried so far/where exactly did you get stuck?

I tried "([^=]*)=([^;]*);", and it worked on the sample string you provided.

Niki
 
WALDO said:
[VB.Net]
Imports System.Text.RegularExpressions

Dim strPattern As String = "(?<name>.+)\=(?<value>.+)\;"
Dim regExp As New RegEx(strPattern, RegExOptions.ExplicitCapture)

1. neither '=' nor ';' need an escape - they aren't metacharacters
2. + is a greedy quantifier, so this expression will never work if there's
more than one value pair
3. who says "value" must have at least one character?
Sometimes the greatest solutions come from the simplest logic.

Complex problems have simple, obvious solution that don't work.
Being told "No" is merely the incentive to do it anyway.

Maybe... anyway, I'd still rather test my code to see if it works...

Niki
 
M. D'Costa said:
Hello,

I am trying to write a regular expression to parse the string:

Database=TestDB;Data Source=server;User Id=guest;Password=;

Am doing this to extract the values after the equals sign( =) and before
the semicolon(;)
and I want the values stored in variables that are before the equals
sign.....

I have been semi-successful. Help will be appreciated.

Thanks,
Marise
It is quite possible to do it with a regular expression, but why not use the
Split method of the string class?
 
Back
Top