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....
16 1612 Frinavale 9,735
Recognized Expert Moderator Expert
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
or you could use regular expressions: - Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
-
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 9,735
Recognized Expert Moderator Expert
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 :)
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...
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 ?
or you could use regular expressions: - Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
-
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
u have to use
System.Text.RegularExpressions;
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
The symbol after the ?i at the beginning should be a colon (:) not a semi-colon (;)...
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
Oops, I missed another omission in your match string: - 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...
Oops, I missed another omission in your match string: - 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...
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: - private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
-
-
public string GetSetting(string SettingName)
-
{
-
return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
-
}
From which you could extract each setting as easily as: - public main()
-
{
-
string strDS = GetSetting("Data Source");
-
string strProv = GetSetting("Provider");
-
string strLoc = GetSetting("Location");
-
}
Piece of cake really...
Well a one line solution would just involve putting the regex into a method something like: - private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
-
-
public string GetSetting(string SettingName)
-
{
-
return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
-
}
From which you could extract each setting as easily as: - public main()
-
{
-
string strDS = GetSetting("Data Source");
-
string strProv = GetSetting("Provider");
-
string strLoc = GetSetting("Location");
-
}
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.
Not so much an extra setting as a ) in the wrong place: - public static string GetSetting(string SettingName)
-
{
-
return Regex.Match(SettingsString, String.Format("(?i:(?<={0}=)[^;]+(?=;|$))", SettingName)).Value;
-
}
-
-
static void Main(string[] args)
-
{
-
string strDS = GetSetting("Data Source");
-
string strProv = GetSetting("Provider");
-
string strLoc = GetSetting("Location");
-
-
Console.WriteLine(strDS);
-
Console.WriteLine(strProv);
-
Console.WriteLine(strLoc);
-
Console.ReadKey();
-
}
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...
It's Working ...
Great..
Thanks... a lot once again
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Larry C |
last post by:
Hello,
I have an XML doc and I am trying to write a utility thath will allow me to
change a particular word with my "title" section of theXML doc. I would
like to look for a "word" in the title...
|
by: Scott M. Lyon |
last post by:
I'm trying to figure out the best way (considering there could be instances
where I get a lot of data in this XML, and I want to minimize any slowdowns)
to extract the value of one particular node...
|
by: .NET Developer |
last post by:
Hello,
I'm trying to write a RegEx that will find all occurances of a particular
type of HTML anchor <a> element in a big block of HTML. Here are the pattern
requirements - they consist of certain...
|
by: CodeRazor |
last post by:
I am reading an xml file from a URL. I was originally doing this using the
XmlDocument class. But this was very slow. The XmlTextReader is meant to be
much quicker for forward only retrieval of...
|
by: Hari |
last post by:
Hi all,
I need to get a part of string which follows a pattern 'addr='
For example:
a)test="192.168.1.17:/home/ankur/nios_fson/mnt/tmptype
nfs(rw,addr=192.168.1.17)"
| |
by: venkat9200 |
last post by:
I need to find out how many bytes a particulat language text takes.
In my application i have a display limit for the unicode string.
So i need to find the no of bytes a particular language text...
|
by: Cplusplusbeginner |
last post by:
I need to write a program to search a particular string in a specific file.e.g if the file is abc.dat which contains names of students in a class, I need to search for a particular name say John in...
|
by: ajithmanmadhan |
last post by:
hi pls help me out in inserting text into a richtextbox.
the situation is like this::
i want to insert a string say "hello" at a particular location in the rich textbox.
The particular...
|
by: dsudhakara |
last post by:
In my queues.conf file my text message are stored here .I want to retrieve particular campaign modify some words in particular campaign and return to save the same queues.conf file .Plz give me...
|
by: nityaprashant |
last post by:
Hello..
could u plz help me?
i have template column inside grid view..
in that one link button called Remove this? when i click on that link button confirmation box like "Are u sure u want...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |