简体   繁体   English

正则表达式C#模式匹配

[英]Regex c# pattern match

Regex pattern(parent): ([Az]{1,})-([az]{1,})= this pattern finds out(eg:mid-night). 正则表达式模式(父项): ([Az]{1,})-([az]{1,})=此模式可以找出(例如:午夜)。

Regex pattern (child): Need to Know regex pattern for finding words(midnight) and (mid night). 正则表达式模式(儿童):需要了解正则表达式模式以查找单词(午夜)和(午夜)。

I need Regex pattern for finding those words. 我需要使用正则表达式模式来查找这些单词。

[az]+ ?[az]+ matches both midnight and mid night . [az]+ ?[az]+两场比赛midnightmid night

Middle " ?" 中部" ?" matches zero or one space characters. 匹配零个或一个空格字符。

I assume you're using the parenthesis to regroup the words after. 我假设您使用括号将后面的单词重新组合。 I'm not sure what you're looking for with regard to including capitalization. 对于包含大写字母,我不确定您要查找什么。 ([Az]+)[ ]?([az]+) will match midnight , MIDNIGHt , mid night , Mid night , MID night , etc. ([Az]+)[ ]?([az]+)将匹配midnightMIDNIGHtmid nightMid nightMID night等。

If you're looking to match mid-night as well, use ([Az]+)[- ]?([az]+) . 如果您也想与午夜比赛,请使用([Az]+)[- ]?([az]+)

I'm not sure what your aim is but this regex should work: 我不确定您的目标是什么,但是此正则表达式应该起作用:

([A-z]{1,})[ ]?([a-z]{1,})

Or try this one to match also your parent pattern: 或尝试使用此样式来匹配您的父模式:

([A-z]{1,})[ ]?-?([a-z]{1,})

But still I would' recommend to just split the parent match using - , so: 但我仍然建议仅使用-拆分匹配项,因此:

string parentMatch = "mid-night";
string[] words = parentMatch.Split('-');

Then you would get following output words = { "mid", "night" } , so you could either concatenate them or not 然后您将得到以下输出words = { "mid", "night" } ,因此您可以将它们串联或不串联

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

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