[英]How to Write Custom Regex To Match A String
我正在使用動態短信模板,下面是我的代碼
我的原始短信模板是這樣的
string str="Dear 123123, pls verify your profile on www.abcd.com with below login details: User ID : 123123 Password: 123123 Team- abcd";
在這里123123可以用以下任何東西代替,例如
Dear Ankit, pls verify your profile on www.abcd.com with below login details: User ID : Ankit@123 Password: Ankit@123 Team- abcd
現在我如何確保該字符串匹配除123123之外的所有其他內容,該字符串可以是任何動態值,例如Ankit,Ankit@123
string ss="Dear Ankit, pls verify your profile on www.abcd.com with below login details: User ID : 123 Password: 123 Team- abcd";
Regex Re = new Regex("What Regex Here");
if(Re.IsMatch(ss))
{
lblmsg.Text = "Matched!!!";
}
else
{
lblmsg.Text = "Not Matched!!!";
}
@"^Dear (\S+?), pls verify your profile on www\.abcd\.com with below login details: User ID : (\S+) Password: (\S+) Team- abcd$"
// A ^ to match the start, $ to match the end, \. to make the dots literal
// (\S+) to match "one or more not-whitespace characters" in each of the 123123 places.
// @"" to avoid usual C# string escape rules
// Not sure what happens to your template if the name or password has spaces in.
例如
using System;
using System.Text.RegularExpressions;
class MainClass {
public static void Main (string[] args) {
string ss="Dear Ankit, pls verify your profile on www.abcd.com with below login details: User ID : 123 Password: 123 Team- abcd";
Regex Re = new Regex(@"^Dear (\S+), pls verify your profile on www\.abcd\.com with below login details: User ID : (\S+) Password: (\S+) Team- abcd$");
if(Re.IsMatch(ss))
{
Console.WriteLine ("Matched!!!");
}
else
{
Console.WriteLine ("Not Matched!!!");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.