4

I am scraping some website content which is like this - "Company Stock Rs. 7100".

Now, what i want is to extract the numeric value from this string. I tried split but something or the other goes wrong with my regular expression.

Please let me know how to get this value.

7
  • 1
    is the number always going to be on the end of the string? Commented Mar 20, 2012 at 14:34
  • 1
    Will the numbers always be at the end? Will the text always be "Company Stock".. ? Commented Mar 20, 2012 at 14:35
  • Yes the numbers are always going to be at the end Commented Mar 20, 2012 at 14:36
  • @RobLevine. What difference does it make? He doesn't want only the numbers that in the end. Commented Mar 20, 2012 at 14:42
  • 3
    "but something or the other goes wrong with my regular expression." After asking 117 questions and with a rep of almost 2500, you would think that you would know by now: Post some code to show us what you've tried. Commented Mar 20, 2012 at 14:42

7 Answers 7

15

Use:

var result = Regex.Match(input, @"\d+").Value;

If you want to find only number which is last "entity" in the string you should use this regex:

\d+$

If you want to match last number in the string, you can use:

\d+(?!\D*\d)
Sign up to request clarification or add additional context in comments.

Comments

5
int val = int.Parse(Regex.Match(input, @"\d+", RegexOptions.RightToLeft).Value);

Comments

5

I always liked LINQ:

var theNumber = theString.Where(x => char.IsNumber(x));    

Though Regex sounds like the native choice...

6 Comments

@KirillPolishchuk. You're right, I just removed it... =) thanks
What about string like: 1 a 2?
@KirillPolishchuk. You will get 12. I wrote Regex is the native choice. I just showed another way with LINQ. (I would use Regex, if that's your point...)
But this returns IEnumerable<char>() but not a string, isn't it?
@Flowerking. I think you can write new string(theString) to get it as a string.
|
1

This code will return the integer at the end of the string. This will work better than the regular expressions in the case that there is a number somewhere else in the string.

    public int getLastInt(string line)
    {
        int offset = line.Length;
        for (int i = line.Length - 1; i >= 0; i--)
        {
            char c = line[i];
            if (char.IsDigit(c))
            {
                offset--;
            }
            else
            {
                if (offset == line.Length)
                {
                    // No int at the end
                    return -1;
                }
                return int.Parse(line.Substring(offset));
            }
        }
        return int.Parse(line.Substring(offset));
    }

1 Comment

"This will work better than the regular expressions in the case that there is a number somewhere else in the string." - in what sense? One can easily make a regex to only match numbers at the end of the string.
1

If your number is always after the last space and your string always ends with this number, you can get it this way:

str.Substring(str.LastIndexOf(" ") + 1)

Comments

1

Here is my answer ....it is separating numeric from string using C#....

    static void Main(string[] args)
    {
        String details = "XSD34AB67";
        string numeric = "";
        string nonnumeric = "";
        char[] mychar = details.ToCharArray();
        foreach (char ch in mychar)
        {
            if (char.IsDigit(ch))
            {

                numeric = numeric + ch.ToString();
            }
            else
            {
                nonnumeric = nonnumeric + ch.ToString();
            }
        }

        int i = Convert.ToInt32(numeric);
        Console.WriteLine(numeric);
        Console.WriteLine(nonnumeric);
        Console.ReadLine();



    }
}

}

Comments

0

You can use \d+ to match the first occurrence of a number:

string num = Regex.Match(input, @"\d+").Value;

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.