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...
Printable View
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...
.vnet 4.0
c# 2010
C# 2010
.net 4.0
efgh.SubString(...)
efgh.StartsWith(...)
and
efgh.Split(...)
should give you everything you need.
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).
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();
}
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();
}