简体   繁体   中英

Regex finding the pattern

How to find the below pattern is there or not in a given string using Regex or any other methods in C#

NAME <some text here> RANK   

The spaces coming after NAME,before RANK and after RANK should be considered. The text between <> will vary.

采用:

var result = Regex.IsMatch(input, @"NAME\s.*?\sRANK\s{3}");

Take a look at http://www.regular-expressions.info/ , it's a pretty useful resource for learning Regex. In C#, take a look at the System.Text.RegularExpressions namespace, specifically the System.Text.RegularExpressions.Regex class.

From the requirements, these might work (I'm still not sure about some of the boundry conditions in your description).

NAME(?=[ ]).+?(?<=[ ])RANK(?=[ ])

or

(?<=^|[ ])NAME(?=[ ]).+?(?<=[ ])RANK(?=[ ]|$)

The dot doesen't include newlines. It can be replaced with [\\s\\S] or you can simply add (?s) at the begining to include them.

Simply Use :

if(Regex.IsMatch(input, "NAME .* RANK")) 
{ }

或者只是做

string s = "NAME sdhfg hgfjh sjfg gf RANK".Replace("NAME ","").Replace(" RANK","");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM