how to get particular string in .net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dipalipatel
    New Member
    • Nov 2008
    • 21

    how to get particular string in .net

    hi

    i have following string

    string1="provid er=pervasive oledb; datasource=test ; location=mycomp uter"

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

    and string3=mycompu ter (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
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      or you could use regular expressions:

      Code:
      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:(?<=locatio n=)[^;]+(?=;)"

      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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        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 :)

        Comment

        • balabaster
          Recognized Expert Contributor
          • Mar 2007
          • 798

          #5
          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...

          Comment

          • kunal pawar
            Contributor
            • Oct 2007
            • 297

            #6
            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 ?

            Comment

            • dipalipatel
              New Member
              • Nov 2008
              • 21

              #7
              Originally posted by balabaster
              or you could use regular expressions:

              Code:
              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:(?<=locatio n=)[^;]+(?=;)"

              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

              Comment

              • kunal pawar
                Contributor
                • Oct 2007
                • 297

                #8
                u have to use
                System.Text.Reg ularExpressions ;

                Comment

                • dipalipatel
                  New Member
                  • Nov 2008
                  • 21

                  #9
                  Originally posted by kunal pawar
                  u have to use
                  System.Text.Reg ularExpressions ;

                  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

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

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

                    Comment

                    • dipalipatel
                      New Member
                      • Nov 2008
                      • 21

                      #11
                      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=Perv asiveOLEDB;Data Source=BBData;L ocation=MyCompu ter"

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

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

                      Can you help with this?

                      Thanks

                      Comment

                      • balabaster
                        Recognized Expert Contributor
                        • Mar 2007
                        • 798

                        #12
                        Oops, I missed another omission in your match string:

                        Code:
                        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...

                        Comment

                        • dipalipatel
                          New Member
                          • Nov 2008
                          • 21

                          #13
                          Originally posted by balabaster
                          Oops, I missed another omission in your match string:

                          Code:
                          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...

                          Comment

                          • balabaster
                            Recognized Expert Contributor
                            • Mar 2007
                            • 798

                            #14
                            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:

                            Code:
                            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:
                            Code:
                            public main()
                            {
                                string strDS = GetSetting("Data Source");
                                string strProv = GetSetting("Provider");
                                string strLoc = GetSetting("Location");
                            }
                            Piece of cake really...

                            Comment

                            • dipalipatel
                              New Member
                              • Nov 2008
                              • 21

                              #15
                              Originally posted by balabaster
                              Well a one line solution would just involve putting the regex into a method something like:

                              Code:
                              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:
                              Code:
                              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.Reg ularExpressions .RegexOptions ..
                              And in Soutions you pass 3 Strings, So it gives an error regarding that 3rd arg.

                              Comment

                              Working...