如何编写自定义Regex以匹配字符串
本文关键字:字符串 Regex 何编写 自定义 | 更新日期: 2023-09-27 18:16:49
我正在开发一个动态短信模板,下面是我的代码
我的原始短信模板像这个
string str="Dear 123123, pls verify your profile on www.abcd.com with below login details: User ID : 123123 Password: 123123 Team- abcd";
这里123123可以被任何东西取代,比如下面的ex-
Dear Ankit, pls verify your profile on www.abcd.com with below login details: User ID : Ankit@123 Password: Ankit@123 Team- abcd
现在,我如何确保字符串匹配除123123之外的所有内容,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!!!");
}
}
}
在repl.it 上在线试用