Connecting Tech Pros Worldwide Forums | Help | Site Map

how to get particular string in .net

Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#1: Nov 15 '08
hi

i have following string

string1="provider=pervasive oledb; datasource=test; location=mycomputer"

now i want to fetch the string
string2=test (i.e.= whatever in my datasource=)

and string3=mycomputer (i.e.=whatever in my location=)

also the string in string1 is not fixed
this is windows application in vb.net

Can anyone help me in finding the solution...

thanks in advance....

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,133
#2: Nov 16 '08

re: how to get particular string in .net


Use the String.Split() method and split the string on ";". This will give you an array of Strings and you can do with them as you please.

-Frinny
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#3: Nov 16 '08

re: how to get particular string in .net


or you could use regular expressions:

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
  2. Dim str2 As String = Regex.Match(str1, "?i:(?<=datasource=)[^;]+(?=;)").Value
You could use the same idea for any of the settings, just replace datasource with the new item... i.e. for the location:

"?i:(?<=location=)[^;]+(?=;)"

Just to give you some clue as to what's going on, check out the regular expression cheat sheet for .NET found at: http://regexlib.com/CheatSheet.aspx
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,133
#4: Nov 16 '08

re: how to get particular string in .net


Quote:

Originally Posted by balabaster

or you could use regular expressions:

I don't know why I always forget about regular expressions.
They are very handy especially with Strings.

Thanks Balabaster :)
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#5: Nov 16 '08

re: how to get particular string in .net


No problem... I can't count the number of times though where I've thought immediately to use regular expressions for something and then after having spent an hour trying to perfect a regular expression, I could've done something far quicker to implement... like by splitting a string into an array...
kunal pawar's Avatar
Needs Regular Fix
 
Join Date: Oct 2007
Location: Pune, India
Posts: 297
#6: Nov 17 '08

re: how to get particular string in .net


I guess this is ur setting so ur application.
So datasource and other things are changed after installation.
So why ur not use app.config ?
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#7: Nov 19 '08

re: how to get particular string in .net


Quote:

Originally Posted by balabaster

or you could use regular expressions:

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
  2. Dim str2 As String = Regex.Match(str1, "?i:(?<=datasource=)[^;]+(?=;)").Value
You could use the same idea for any of the settings, just replace datasource with the new item... i.e. for the location:

"?i:(?<=location=)[^;]+(?=;)"

Just to give you some clue as to what's going on, check out the regular expression cheat sheet for .NET found at: http://regexlib.com/CheatSheet.aspx

Hi,
Thanks for your Answer.
But i am not sure which dll i need to add in my code so that i can get that method Regex.match.....
Can you send me that dll??

Thanks
Dipali
kunal pawar's Avatar
Needs Regular Fix
 
Join Date: Oct 2007
Location: Pune, India
Posts: 297
#8: Nov 19 '08

re: how to get particular string in .net


u have to use
System.Text.RegularExpressions;
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#9: Nov 20 '08

re: how to get particular string in .net


Quote:

Originally Posted by kunal pawar

u have to use
System.Text.RegularExpressions;


Thanks Kunal...
I tryed that and it gives me the follong error

parsing "?i;(<=Data Source=)[^;]+(?=;)" - Quantifier {x,y} following nothing.


Can you help with this?

Thanks
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#10: Nov 20 '08

re: how to get particular string in .net


The symbol after the ?i at the beginning should be a colon (:) not a semi-colon (;)...
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#11: Nov 20 '08

re: how to get particular string in .net


Quote:

Originally Posted by balabaster

The symbol after the ?i at the beginning should be a colon (:) not a semi-colon (;)...

Hi

I did that but still i get the error. So, here is what i did:

My Original string:
mystr ="Provider=PervasiveOLEDB;Data Source=BBData;Location=MyComputer"

Part of string that i want as below:
Getstr = System.Text.RegularExpressions.Regex.Match(mystr, "?i:(<=Data Source=)[^;]+(?=;)").Value;

Error:
parsing "?i:(<=Data Source=)[^;]+(?=;)" - Quantifier {x,y} following nothing.

Can you help with this?

Thanks
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#12: Nov 20 '08

re: how to get particular string in .net


Oops, I missed another omission in your match string:

Expand|Select|Wrap|Line Numbers
  1. string MatchString = "(?i:(?<=Data Source=)[^;]+(?=;))";
Replace your match string with that...you didn't copy it right originally...

You missed a ? before <=Data Source and you need to wrap your string in another set of parenthesis...
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#13: Nov 20 '08

re: how to get particular string in .net


Quote:

Originally Posted by balabaster

Oops, I missed another omission in your match string:

Expand|Select|Wrap|Line Numbers
  1. string MatchString = "(?i:(?<=Data Source=)[^;]+(?=;))";
Replace your match string with that...you didn't copy it right originally...

You missed a ? before <=Data Source and you need to wrap your string in another set of parenthesis...

Hey, You are great!!!

Thanks a Lot for your Solution.
Actually i was looking for a 1 line solution instead of Split and all function.
I Appreciate your help

Once again Thanks a lot...
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#14: Nov 20 '08

re: how to get particular string in .net


Quote:

Originally Posted by dipalipatel

Hey, You are great!!!

Thanks a Lot for your Solution.
Actually i was looking for a 1 line solution instead of Split and all function.
I Appreciate your help

Once again Thanks a lot...

Well a one line solution would just involve putting the regex into a method something like:

Expand|Select|Wrap|Line Numbers
  1. private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
  2.  
  3. public string GetSetting(string SettingName)
  4. {
  5.     return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
  6. }
From which you could extract each setting as easily as:
Expand|Select|Wrap|Line Numbers
  1. public main()
  2. {
  3.     string strDS = GetSetting("Data Source");
  4.     string strProv = GetSetting("Provider");
  5.     string strLoc = GetSetting("Location");
  6. }
Piece of cake really...
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#15: Nov 20 '08

re: how to get particular string in .net


Quote:

Originally Posted by balabaster

Well a one line solution would just involve putting the regex into a method something like:

Expand|Select|Wrap|Line Numbers
  1. private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
  2.  
  3. public string GetSetting(string SettingName)
  4. {
  5.     return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
  6. }
From which you could extract each setting as easily as:
Expand|Select|Wrap|Line Numbers
  1. public main()
  2. {
  3.     string strDS = GetSetting("Data Source");
  4.     string strProv = GetSetting("Provider");
  5.     string strLoc = GetSetting("Location");
  6. }
Piece of cake really...


Hi !!
Very Nice Solution
but Regex.Match has only 2 arguments in string, the 3rd one is System.Text.RegularExpressions.RegexOptions ..
And in Soutions you pass 3 Strings, So it gives an error regarding that 3rd arg.
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#16: Nov 20 '08

re: how to get particular string in .net


Not so much an extra setting as a ) in the wrong place:

Expand|Select|Wrap|Line Numbers
  1.         public static string GetSetting(string SettingName) 
  2.         { 
  3.             return Regex.Match(SettingsString, String.Format("(?i:(?<={0}=)[^;]+(?=;|$))", SettingName)).Value; 
  4.         } 
  5.  
  6.         static void Main(string[] args)
  7.         {
  8.             string strDS = GetSetting("Data Source");
  9.             string strProv = GetSetting("Provider");
  10.             string strLoc = GetSetting("Location");
  11.  
  12.             Console.WriteLine(strDS);
  13.             Console.WriteLine(strProv);
  14.             Console.WriteLine(strLoc);
  15.             Console.ReadKey();
  16.         }
I made one slight modification to the match string also - because it wouldn't pick up the last setting if you omitted the closing ";", I added an "|$" after the ";" so that it would check for ; or end of string.

Sorry, usually I try and test my code before posting it, but I didn't have access to Visual Studio when I posted last time...
Newbie
 
Join Date: Nov 2008
Location: Canada
Posts: 21
#17: Nov 20 '08

re: how to get particular string in .net


It's Working ...
Great..

Thanks... a lot once again
Reply