Retrieve Number from String using Regular Expression in C# and VB.Net

Last Reply 15 days ago By dharmendr

Posted 15 days ago

Hi! How I can get only number from text.

 

string selected = "";
int selectedBolsCount = 1;
string selectedValues = "Let me introduce myself∞1My full name is Andrei Andreevich Ivanov∞2I was named after my father and I am very happy about it∞3I was born on April, 7, 1981 in Moscow∞4I come from the family of office workers∞5I am an only child in the family∞6I am a pupil of the eleventh form of a secondary school∞7 I'm finishing school this year∞8I want to enter the University of Moscow∞9That is why I have to study twice as hard as an ordinary pupil∞10I'm not only doing my best at school, I'm taking a preparatory course at the University∞11";
foreach (string item in selectedValues.Split('∞'))
  {
      selected = string.Join(",", selectedValues);
      selectedBolsCount++;
  }

Output result:

1,2,3,4,5,6,7,8,9,10,11

Posted 15 days ago Modified on 15 days ago

Hi PRA,

Refer below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    string selected = "";
    string selectedValues = "Let me introduce myself∞1My full name is Andrei Andreevich Ivanov∞2I was named after my father and I am very happy about it∞3I was born on April, 7, 1981 in Moscow∞4I come from the family of office workers∞5I am an only child in the family∞6I am a pupil of the eleventh form of a secondary school∞7 I'm finishing school this year∞8I want to enter the University of Moscow∞9That is why I have to study twice as hard as an ordinary pupil∞10I'm not only doing my best at school, I'm taking a preparatory course at the University∞11";
    foreach (string item in selectedValues.Split('∞'))
    {
        string number = System.Text.RegularExpressions.Regex.Match(item, @"\d+").Value;
        if (!string.IsNullOrEmpty(number))
        {
            selected += number + ",";
        }
    }
    Response.Write(selected.TrimEnd(','));
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim selected As String = ""
    Dim selectedValues As String = "Let me introduce myself∞1My full name is Andrei Andreevich Ivanov∞2I was named after my father and I am very happy about it∞3I was born on April, 7, 1981 in Moscow∞4I come from the family of office workers∞5I am an only child in the family∞6I am a pupil of the eleventh form of a secondary school∞7 I'm finishing school this year∞8I want to enter the University of Moscow∞9That is why I have to study twice as hard as an ordinary pupil∞10I'm not only doing my best at school, I'm taking a preparatory course at the University∞11"
    For Each item As String In selectedValues.Split("∞"c)
        Dim number As String = System.Text.RegularExpressions.Regex.Match(item, "\d+").Value
        If Not String.IsNullOrEmpty(number) Then
            selected += number & ","
        End If
    Next
    Response.Write(selected.TrimEnd(","c))
End Sub

Output

1,2,3,4,5,6,7,8,9,10,11