Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Finding Sentence using regular expressions with .net


Finding Sentence using regular expressions with .net

In the field of linguistics, a sentence —an expression in natural language— is often defined to indicate a grammatical and lexical unit consisting of one or more words that represent distinct concepts. A sentence can include words grouped meaningfully to express a statement, question, exclamation, request or command.

All the sentences contins group of words ending with .(punctuation mark), ?(question mark), !(exclamation mark) or : (colon) etc. In this regular expression we are going to find out sentences, means group of words ending with .(punctuation mark), ?(question mark), !(exclamation mark) or : (colon) etc.

Regular Expression Pattern

(?sx-m)[^\r\n].*?(?:(?:\.|\?|!|\:)\s)

A description of the regular expression:

Change options within the enclosing group [sx-m]
Turn OFF Multiline option
Turn ON Single Line option
Turn ON Ignore Pattern Whitespace option
Any character that is NOT in this class: [\r\n]
Any character, any number of repetitions, as few as possible
Match expression but don’t capture it. [(?:\.|\?|!|\:)\s]
(?:\.|\?|!|\:)\s
Match expression but don’t capture it. [\.|\?|!|\:]
Select from 4 alternatives
Literal .
Literal ?
!
Literal :
Whitespace

Sucessful Matches

This is a test.
What you want?
That’s great!
as shown below:

How It Works

This regular expression will check for any combination of words ending with .(punctuation mark), ?(question mark), !(exclamation mark) or : (colon).

ASP.NET

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Finding Sentence using regular expressions with .net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: enter some characters</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server" ControlToValidate="txtInput" ErrorMessage="Word can not end with.(punctuation mark), ?(question mark), !(exclamation mark) or : (colon)" ValidationExpression="\(?sx-m)[^\r\n].*?(?:(?:\.|\?|!|\:)\s)">
</asp:RegularExpressionValidator><br />
<asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True" Text="Submit"></asp:Button>
</div>
</form>
</body>
</html>

C#.NET

//use System.Text.RegularExpressions befour using this function
public bool vldRegex(string strInput)
{
//create Regular Expression Match pattern object
Regex myRegex = new Regex(@"(?sx-m)[^\r\n].*?(?:(?:\.|\?|!|\:)\s)");
//boolean variable to hold the status
bool isValid = false;
if (string.IsNullOrEmpty(strInput))
{
isValid = false;
}
else
{
isValid = myRegex.IsMatch(strInput);
}
//return the results
return isValid;
}

VB.NET

‘Imports System.Text.RegularExpressions befour using this function
Public Function vldRegex(ByVal strInput As String) As Boolean
‘create Regular Expression Match pattern object
Dim myRegex As New Regex("(?sx-m)[^\r\n].*?(?:(?:\.|\?|!|\:)\s)")
‘boolean variable to hold the status
Dim isValid As Boolean = False
If strInput = "" Then
isValid = False
Else
isValid = myRegex.IsMatch(strInput)
End If
‘return the results
Return isValid
End Function

 

Chetan love blogging. He regularly blogs at http://www.tipsntracks.com. You can connect with Chetan on Twitter, Facebook and Google Plus...

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image