2016-11-08 2 views
0

Я работаю на Dynamic SMS Шаблоны ниже мой кодКак написать пользовательский Regex Чтобы соответствовать строке

Мой Оригинал SMS шаблон, как этот

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 : [email protected] Password: [email protected] Team- abcd 

Теперь Как я могу убедиться в том, что строка соответствует все же, кроме 123123, который может быть любая вещь, динамическое значение, как Ankit,[email protected]

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!!!"; 
     } 

ответ

1
@"^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!!!"); 
     } 
    } 
} 

Try it online at repl.it

+0

Благодаря @TessellatingHeckler его работает отлично ... –

Смежные вопросы