简体   繁体   English

正则表达式(匹配单词+某个数字)C#

[英]Regular Expression (match word + some number) C#

Sorry doing this the fast way of asking all the regex experts out there :) 抱歉这样快速询问所有正则表达式专家:)

What regex for C# can I use that matches the end of a word + some number 我可以使用哪种正则表达式匹配单词的结尾+某个数字

Eg. 例如。 End of string matches with "Story" + 0 or more numbers 字符串结尾与“Story”+ 0或更多数字匹配

"ThisIsStory1"    = true
"ThisIsStory2000" = true
"ThisIsStory"     = true
"Story1IsThis"    = false
"IsStoryThis1"    = false

Hope this makes sense 希望这是有道理的

A regular expression that I could plug into C# would be fantastic. 我可以插入C#的正则表达式非常棒。

Thanks 谢谢

You'll need something like this Story[0-9]*$ 你需要这样的Story[0-9]*$

So it matches for story (ignoring anything before it, you may need to add more), then for 0 or more numbers between story and the end of the string. 所以它匹配故事(忽略它之前的任何内容,你可能需要添加更多),然后在故事和字符串结尾之间的0或更多数字。

Try [A-Za-z]*Story[0-9]* . 试试[A-Za-z]*Story[0-9]*

If you want to check against a whole line, ^[A-Za-z]*Story[0-9]*$ . 如果你想检查整行, ^[A-Za-z]*Story[0-9]*$

to match number of characters before story and then story???? 在故事和故事之前匹配字符数???? use this: 用这个:

"[A-Za-z]*(s|S)tory\d*$"
string compare = "story1withANumber";
Regex regex = new Regex(@"[A-Za-z]+Story[0-9]*");

if (regex.IsMatch(compare))
{ 
    //true
}

regex = new Regex("[0-9]");

if (regex.IsMatch(compare))
{ 
    //true
}

A .NET regex that meets your criteria is: 符合您标准的.NET正则表达式是:

Story\d*\b

(Use the Regex.IgnoreCase option if desired.) (如果需要,请使用Regex.IgnoreCase选项。)

\\b matches the word boundary, so Story1IsThis will not match, but ThisIsAStory1 will. \\b匹配单词边界,所以Story1IsThis将不匹配,但ThisIsAStory1将匹配。 The word 'Story' does not need to end the string, so the string "ThisIsAStory234 ThisIsNot" will match on Story234 . 单词'Story'不需要结束字符串,因此字符串"ThisIsAStory234 ThisIsNot"将匹配Story234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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