2

I have alphanumeric string list. For example:

1A
2B
7K
10A

I want to get only the numeric part and then compare them, if it is less than 10 I need not to add it in another list. What I want to know the regex to split the numeric part from the string. Any help. What I have done till now is:

 if (x == y) // also handles null
            return 0;
        if (x == null)
            return -1;
        if (y == null)
            return +1;

        int ix = 0;
        int iy = 0;
        while (ix < x.Length && iy < y.Length)
        {
            if (Char.IsDigit(x[ix]) && Char.IsDigit(y[iy]))
            {
                // We found numbers, so grab both numbers
                int ix1 = ix++;
                int iy1 = iy++;
                while (ix < x.Length && Char.IsDigit(x[ix]))
                    ix++;
                while (iy < y.Length && Char.IsDigit(y[iy]))
                    iy++;
                string numberFromX = x.Substring(ix1, ix - ix1);
                string numberFromY = y.Substring(iy1, iy - iy1);

                // Pad them with 0's to have the same length
                int maxLength = Math.Max(
                    numberFromX.Length,
                    numberFromY.Length);
                numberFromX = numberFromX.PadLeft(maxLength, '0');
                numberFromY = numberFromY.PadLeft(maxLength, '0');

                int comparison = _CultureInfo
                    .CompareInfo.Compare(numberFromX, numberFromY);
                if (comparison != 0)
                    return comparison;
            }
            else
            {
                int comparison = _CultureInfo
                    .CompareInfo.Compare(x, ix, 1, y, iy, 1);
                if (comparison != 0)
                    return comparison;
                ix++;
                iy++;
            }
        }

But I don't want to be so complex in my approach. So I need a regex to split.

3
  • what are x and y..why not parse them to int and then compare..Show us your complete code Commented Oct 10, 2013 at 6:16
  • From the question its obvious that x and y will be items from the list like 1A, 2B etc Commented Oct 10, 2013 at 6:46
  • you can use below address in stackoverflow stackoverflow.com/questions/3720012/… Commented Oct 26, 2014 at 7:55

4 Answers 4

3

Try IsDigit method of char

var number = int.Parse(new string(someString.Where(char.IsDigit).ToArray()));
if(number<10)
{
   someList.Add(number);
}

using All and IsDigit you can take only numeric part of the string, then parse it to int and compare :) There is no need to use Regexes

Sign up to request clarification or add additional context in comments.

2 Comments

Its the best way but its not working, syntax error, where my string is like "7K,1B".
+1 for the updated code and it is working, let me test properly and I will accept as answers as well. Thank you so much in advance. :)
2

You can use the code below to split an input string and get the result of number group and alpha group. If one group is not there, the result will be empty string.

string input = "10AAA";
Match m = Regex.Match(input, @"(\d*)(\D*)");

string number = m.Groups[1].Value;
string alpha = m.Groups[2].Value;

Comments

1

you can try it with this one:

  string txt="10A";
  string re1="(\\d+)";  // Integer Number 1

  Regex r = new Regex(re1);
  Match m = r.Match(txt);

1 Comment

You can remove both Options without harm. You have no letters, so IgnoreCase is useless, you have no . so Singleline is useless.
0

Are you trying to do this?

int num;
string stringWithNumbers = "10a";
if (int.TryParse(Regex.Replace(stringWithNumbers, @"[^\d]", ""), out num))
{
    //The number is stored in the "num" variable, which would be 10 in this case.
    if (num >= 10)
    {
        //Do something
    }

}
else
{
    //Nothing numeric i the string
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.