-
December 16th, 2011, 12:18 PM
#1
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...
-
December 16th, 2011, 12:19 PM
#2
Re: Help in extracting string
-
December 16th, 2011, 12:19 PM
#3
Re: Help in extracting string
-
December 16th, 2011, 02:11 PM
#4
Re: Help in extracting string
efgh.SubString(...)
efgh.StartsWith(...)
and
efgh.Split(...)
should give you everything you need.
-
December 16th, 2011, 06:25 PM
#5
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.
-
December 19th, 2011, 12:33 PM
#6
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();
}
-
December 19th, 2011, 01:52 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|