به عنوان مثال برای ارسال یک پیام تحت شبکه که باید گیرنده و فرستنده و متن پیام ارسال شود پردازش متن کمی دشوار است، باید انتهای فیلد مورد نظر را با علامتی خاص تعیین کنیم؛ اما با استفاده از عبارات با قاعده ارسال چندین مقدار بسیار آسان می شود.
ابتدا ما تعداد فیلد ها و اسمی برای آنها انتخاب می کنیم، به عنوان مثال برای ارسال یک پیام:
1. فرستنده
2. گیرنده
3. متن پیام
پس از تعیین عبارت باید یک الگو (Pattern) برای خود بسازیم که در زیر شکل کلی یک نمونه عبارت با قاعده را مشاهده می کنید:
String1=(.+?);String2=(.+?);String3=(.+?);
کد ها بصورت زیر است:
Imports System.Text.RegularExpressions Dim Pattern As String = "String1=(.+?);String2=(.+?);String3=(.+?);" Dim Expression As String = TextBox1.Text Dim SearchMatch As Match = Regex.Match(Expression, Pattern, RegexOptions.IgnoreCase) If SearchMatch.Success = True Then Dim Match1 As String = SearchMatch.Groups(1).Value Dim Match2 As String = SearchMatch.Groups(2).Value Dim Match3 As String = SearchMatch.Groups(3).Value TextBox2.Text = Match1 TextBox3.Text = Match2 TextBox4.Text = Match3 Else MsgBox("No match found") End If
در سی شارپ:
string Pattern = "String1=(.+?);String2=(.+?);String3=(.+?);"; string Expression = TextBox1.Text; Match SearchMatch = Regex.Match(Expression, Pattern, RegexOptions.IgnoreCase); if (SearchMatch.Success == true) { string Match1 = SearchMatch.Groups[1].Value; string Match2 = SearchMatch.Groups[2].Value; string Match3 = SearchMatch.Groups[3].Value; TextBox2.Text = Match1; TextBox3.Text = Match2; TextBox4.Text = Match3; } else { MessageBox.Show("No match found"); }