CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2011
    Posts
    3

    Help in extracting string

    I need a function that can extract string starting with "$" and ending with " ' " or "," or nothing

    Suppose string is

    efgh = '$EXPCH.nmo' AND tree = $Name, tree = $pcd

    it should return me
    EXPCH.nmo
    Name
    pcd

    Any help please...

  2. #2
    Join Date
    Dec 2011
    Posts
    3

    Re: Help in extracting string

    .vnet 4.0
    c# 2010

  3. #3
    Join Date
    Dec 2011
    Posts
    3

    Re: Help in extracting string

    C# 2010
    .net 4.0

  4. #4
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Help in extracting string

    efgh.SubString(...)
    efgh.StartsWith(...)

    and

    efgh.Split(...)

    should give you everything you need.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Help in extracting string

    Nikel has the right idea to just write it. However, regular expressions were designed for this type of processing; see http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx

    (However, they can be a little tricky to use; they're not as intuitive as other elements of the .NET library).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Dec 2011
    Posts
    61

    Re: Help in extracting string

    Code:
    using System.Text.RegularExpressions;
    
    public string GetMatches()
    {
                Regex regex = new Regex(@"\$[a-zA-Z\.]+[',]*");
                string t = @"efgh = '$EXPCH.nmo' AND tree = $Name, tree = $pcd";
                MatchCollection matches = regex.Matches(t);
                StringBuilder sb = new StringBuilder();
                foreach (Match m in matches)
                {
                    sb.AppendLine(m.Value.Trim(new char[]{' ', '\'', ',', '$'}));
                }
                return sb.ToString();
    }

  7. #7
    Join Date
    Dec 2011
    Posts
    61

    Re: Help in extracting string

    just figured out a better version without the need to trim, and also fixed a bug:

    Code:
    using System.Text.RegularExpressions;
    
    public string GetMatches()
    {
                Regex regex = new Regex(@"\$[a-zA-Z\.]+(?=[',\s]|$)");
                string t = @"efgh = '$EXPCH.nmo' AND tree = $Name, tree = $pcd";
                MatchCollection matches = regex.Matches(t);
                StringBuilder sb = new StringBuilder();
                foreach (Match m in matches)
                {
                    sb.AppendLine(m.Value);
                }
                return sb.ToString();
    }
    Last edited by Silent Sojourner; December 19th, 2011 at 02:18 PM. Reason: tweak code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured