How to find character position in word using regular expression?


In this code snippet I am going to explain about to find character position in a string, split values based on space and find what are the numbers present inside of the sentence etc. This code is used may be in your project this code snippet is help you.

Description


Below three techniques are used to find character position, split and find number in between words.

1) How to find first integer position in a string value.


In this part we are store values in the string variable. That string variable contain mixed of alpha and numeric character. Using regular expression to find that first integer position like below


//Find first integer value in the string
string s = null;

//Assign values in the variable
s = "ravi123";

//declare variable to get position
int i;

Regex re = new Regex("\\d+");

Match m = re.Match(s);
if (m.Success)
{
i = m.Index;
Response.Write("String value : " + s + "<br/>");
Response.Write("First Integer found in that variable postition: " + i + "<br/><br/><br/>");
}



2) How to find split words based on the space using regular expression?


In this part we are stored sentence in a string variable I have split that each word and write in the web page with position using regular expression like below code

//Split spaces using regular expression and display it in the web page
string d = null;
string[] d1 = null;

d = "This is example of Split based on space";
Response.Write("String value : " + d + "<br/>");

d1 = Regex.Split(d, " +");
d="";
int k = 0;
foreach( string s1 in d1)
{
d = d + "Position : " + k + " " + s1 + "<br/>";
k+= 1;
}
Response.Write(d+ "<br/>");


3) How to find numbers present in between sentence?


In this part we are stored sentence in a string variable with combination of words and numbers. Using regular expression to find only what are numbers are present in the sentence like below code

//get only integer values in the values using regular expression
string t1 = "Find only numbers (4) in bettween (5) this values";
string[] t2 = null;
t2 = Regex.Split(t1, "\\D+");
t1 = "";
foreach (string t in t2)
{
t1 = t1 + " " + t;
}
Response.Write("Numbers found :" + t1 + "");



Full Source code : Code Behind



using System.Text.RegularExpressions;

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

Response.Write(">b>REGULAR EXPRESSION TECHNIQUES EXAMPLES>/b>");
Response.Write(">hr/>");

//Find first integer value in the string
string s = null;

//Assign values in the variable
s = "ravi123";

//declare variable to get value
int i;

Regex re = new Regex("\\d+");
Match m = re.Match(s);
if (m.Success)
{
i = m.Index;
Response.Write("1) String value : " + s + ">br/>");
Response.Write("First Integer found in that variable postition: " + i + ">br/>>br/>");
}

//Split spaces using regular expression and display it in the web page
string d = null;
string[] d1 = null;
Response.Write(d + ">hr/>");
d = "This is example of Split based on space";
Response.Write("2) String value : " + d + ">br/>");

d1 = Regex.Split(d, " +");
d="";
int k = 0;
foreach( string s1 in d1)
{
d = d + "Position : " + k + " " + s1 + ">br/>";
k+= 1;
}

Response.Write(d+ ">br/>");
Response.Write(">hr/>");

//get only integer values in the values using regular expression
string t1 = "Find only numbers (4) in bettween (5) this values";
string tmp = t1;
string[] t2 = null;
t2 = Regex.Split(t1, "\\D+");
t1 = "";
foreach (string t in t2)
{
t1 = t1 + " " + t;
}
Response.Write("3) String value" + tmp + ">br/>>br/>");
Response.Write("Numbers found :" + t1 + "");
Response.Write(">hr/>");
}
}

Output
The output of the above code look like this
RegEx_Output

Conclusion:
I hope this code snippet is help you to know about regular expression search index and its operations.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: