Regular expression to get and find the numbers presented at the end of a string
-
Tuesday, May 08, 2012 12:34 PM
Hi Experts,
I am creating a WPF application in which i needed check a string whether it is having any numbers present at the end of the string and also i want to get the numbers.
Ex:
string- adcd123; // This is the string am having. In which i want get the 123 in int variable.
Please help me anybody and help with code would be appreciated
- Edited by Ramesh Velusamy Tuesday, May 08, 2012 12:49 PM
All Replies
-
Tuesday, May 08, 2012 12:42 PM
\d+$It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
- Marked As Answer by Ramesh Velusamy Tuesday, May 08, 2012 1:13 PM
-
Tuesday, May 08, 2012 1:03 PM
Hi Ramesh,
Please find the code..
int number;
var match = Regex.Match(inputString,@"(\d+)$");
if(match != null)
number = int.Parse(match.Groups[1].ToString());Sai Kumar K http://www.santoshtechnologies.com http://saimaterial.wordpress.com
- Marked As Answer by Ramesh Velusamy Tuesday, May 08, 2012 1:13 PM
-
Tuesday, May 08, 2012 1:11 PM
upper example shows using Regular expressions,
but if your stirng(s) is/are static, that means that letters and numbers are always in same lenght, you can use Substring method:
string text = "adcd123"; int num = int.Parse(text.Substring(4,3));
Mitja
-
Tuesday, May 08, 2012 1:14 PM
Hi,
Thank you for your quick response.
Also i would like to know that how to check whether the string contains "Datatime" value at the end of a string.
Please help me anybody.
-Ramesh
-
Tuesday, May 08, 2012 1:20 PM
Hi,
Thank you for your quick response.
Also i would like to know that how to check whether the string contains "Datatime" value at the end of a string.
Please help me anybody.
-Ramesh
Your welcome. You may want to pose this as a new question, but you are going to need to add more constraints. Date / Time display can change based upon how you tell it to format and locale data.It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
-
Tuesday, May 08, 2012 4:24 PM
Hi,
Yes, No problem you may post this as a new query..
-Ramesh
-
Tuesday, May 08, 2012 4:35 PM
To get a date from the string, or simply check if there is any date inside a string (if contains it), can do some expirimental code like:
bool hasDate = false; DateTime dateTime = new DateTime(); string[] inputText = txtWords.Text().Split(" ");//split on a whitespace foreach( String text in inputText ) { try { dateTime = DateTime.Parse( text ); hasDate = true; break;//no need to execute/loop further if you have your date } catch( Exception ex ) { } } //after breaking from the foreach loop, you can check if hasDate=true //if it is, then your user entered a date and you can retrieve it from the dateTime if( hasDate ) { //user entered a date, get it from dateTime } else { //user didn't enter any date }
Mitja

