Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 'list' variable, which contains values such as - ASW+90s, 160, 160S, M45s, 130.5. R+129.

I would like to know, how can I extract strings such as 160s and R+129.
Here is my attempt so far, but I keep experiencing compiling errors:

C#
else if (Int32.TryParse(list, out i))
                               {
                                   string b = "";
                                   b += list == "S";
                                   if (Math.Abs(i) >= 125 && b)
                                   {
                                       string a = "";
                                       a += i.ToString();
                                       row["Value"] = a;
                                   }
                               }


if someone could help to define, the logic required to implement this task further, that would be a great help, to progress forward.

Any help much appreciated.
Thanks
Posted
Comments
Praveen Kumar Upadhyay 27-Jan-15 7:26am    
Be more specific what output you want and what is your condition.
miss786 27-Jan-15 7:35am    
apology for the being clearer.

My if conditions are:
- extract values which are >125 & contains 's' at the end of the string (i.e. 160s)
- extract values which contain alphabet & '+' & number (i.e. R+160)

desired output:
the extracted values, be placed inside 'row["Value"] = ?'

I hope this helps, to clarify my desired output.
Thank you for your help.
Zoltán Zörgő 27-Jan-15 7:26am    
Regexp will be your friend, but better define what do you want to extract, as for me R+129 does differ from M45s or whatever.
miss786 27-Jan-15 7:36am    
thank you for your reply. please see the above reply, for I what i would like to extract from the list variable. Thank you for suggesting 'Regexp', I shall look into that.
Many thanks

Here you have it:
C#
var str = "ASW+90s, 160, 160S, M45s, 130.5, R+129";
var list = str.Split(new string[] {", "}, StringSplitOptions.RemoveEmptyEntries);

var re1 = new Regex(@"^[A-Za-z]\+\d+$");
var re2 = new Regex(@"^(\d+)[sS]$");

var result = from element in list
             let m2 = re2.Match(element)
             where re1.Match(element).Success || (m2.Success && int.Parse(m2.Groups[1].Value) > 125)
             select element;

result.Dump();

This is not pure-regexp, as I am using semantic validation for the second criteria. And it is working on a concrete list/array, but as you can see it is quite simple to split the string into a list/array.
 
Share this answer
 
v2
Comments
miss786 28-Jan-15 5:08am    
APology for the late reply. Thank you very much for providing a great solution. I appreciate your time and help.
Try this,

C#
static void Main(string[] args)
{
    String tmp = @"(\s\b[1-9](([2][5-9])|([3-9][0-9]))[sS])|(\s\b[aA-zZ]\+\d+)";
    String input = "ASW+90s, 160, 160S, M45s, 130.5. R+129";

    Regex exp = new Regex(tmp);
    MatchCollection col = exp.Matches(input);

}
 
Share this answer
 
Comments
Zoltán Zörgő 27-Jan-15 8:32am    
Good! But will match leading spaces, will match 125S too (it is not > 125) and it is limited to 3 digit numbers. Besides the fact that all values in the example are below 999 OP never mentioned this limitation.
_Asif_ 27-Jan-15 23:25pm    
How long would it take to fix these limitations? My intention is to float the idea so rest can fine tune it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900