How do i pull the number from this string "Emample_ColorRevA.Page1"
I want to be able to pull the number 1 using regular expressions, can someone assist me.
"Emample_ColorRevA.Page1" Result would be 1
"Emample_ColorRevB.Page2" Result would be 2
"Emample_ColorRevB.Page234" Result would be 234
How can I only get the number from the strings
Loading

Satish BhatRank #2,496 · 563 reputationPosted Aug 11, 2011, 10:03 PM EDT
public static string GetNumberFromString(string input)
{
// To Match number anywhere in the string e.g. Something123And234
// you will get 123 & 234
// Match _match = Regex.Match(input.Trim(), @"[0-9]+");
// Match only at the end of the string
Match _match = Regex.Match(input.Trim(), @"(\d+)$");
return (_match.Value);
}
string _input = "Emample_ColorRevB.Page234";
string _output = GetNumberFromString(_input); // 234
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 12:32 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Aug 12, 2011, 3:00 AM EDT
Satish BhatRank #2,496 · 563 reputationPosted Aug 12, 2011, 3:51 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 4:39 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 7:11 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 7:11 AM EDT
Also can someone point me to good sight using regular expressions
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 7:13 AM EDT
Also can someone point me to good sight using regular expressions
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 7:17 AM EDT
A good site for learning regular expressions from the ground up is this one:
http://www.regular-expressions.info/tutorial.html
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 8:09 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 8:33 AM EDT
Match _match = Regex.Match(input.Trim(), @"(\d+)$");
Start from "@"(\d+)$"
I understand the input.trim()
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 8:53 AM EDT
The Regex.Match method says if there's a match in the string for the regular expression, then return it as a Match object. The Value property then gets the actual sub-string which is matched.
David SmithRank #999 · 2K reputationPosted Aug 12, 2011, 9:37 AM EDT
you haven't explain vuples yet. lol
VulpesRank #55 · 29.2K reputationPosted Aug 12, 2011, 10:27 AM EDT
Don't forget to mark Satish's post as the accepted answer.
Satish BhatRank #2,496 · 563 reputationPosted Aug 13, 2011, 6:23 AM EDT
Although I am a registered member since 2004, I made little contribution to the site because of whatever the reasons may be. Now because of you guys I am trying to participate whenever I find time. Keep up the good work.
Vuples, you are 100% right in "there's a tendency to always reach for the driver when an iron will do". So darnell follow the KISS design principle, use the solution and mark the answer.
David SmithRank #999 · 2K reputationPosted Aug 13, 2011, 7:22 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 13, 2011, 7:24 AM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 13, 2011, 8:58 AM EDT
David SmithRank #999 · 2K reputationPosted Aug 13, 2011, 12:31 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 13, 2011, 12:56 PM EDT
If you uncheck mine and check his, then it should move the highlight to his post.
David SmithRank #999 · 2K reputationPosted Aug 13, 2011, 1:51 PM EDT