How To Extract Number from string

In this post i will show how to extract number from string using Regular expression.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExtractNumber.aspx.cs" Inherits="ExtractNumber" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:TextBox ID="txtInput" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnExtract" runat="server" Text="ExtractNumber" OnClick="btnExtract_Click" /><br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ExtractNumber : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnExtract_Click(object sender, EventArgs e)
        lblMessage.Text = ExtractNumbers(txtInput.Text);

    public  string ExtractNumbers(string expr)
        return string.Join(null, System.Text.RegularExpressions.Regex.Split(expr, "[^\\d]"));

1 comment: