简体   繁体   English

字符串匹配的正则表达式

[英]Regex expression for string matching

I need to match my string with "*B*[XXX]" .我需要将我的字符串与"*B*[XXX]"匹配。 So for example it could be "MB0[23]" or "DB50.DBB152[128]" .因此,例如它可能是"MB0[23]""DB50.DBB152[128]" At this moment, it's too complicated for me.这一刻,对我来说太复杂了。

Please help.请帮忙。

This will select both of your examples, but unfortunately with such little to work with, I can't be certain that this is what you want.这将选择您的两个示例,但不幸的是,使用的很少,我无法确定这是您想要的。

/\\wB\\d+(?:\\.\\w{3}\\d+)?\\[\\d+\\]/ should give you what you want. /\\wB\\d+(?:\\.\\w{3}\\d+)?\\[\\d+\\]/应该给你你想要的。

With this regex, MB0[23] will be returned, as well as all of DB50.DBB152[128] .使用此正则表达式,将返回MB0[23]以及所有DB50.DBB152[128]

Hope this solves your problem!希望这能解决您的问题!

If I get it right from your example, your format has to contain a capital B and brackets with numbers in it.如果我从您的示例中正确理解,则您的格式必须包含大写 B 和带有数字的括号。 Try to use one of these:尝试使用其中之一:

This for strings that contain a "B" and brackets after it (with optional contents):这对于包含“B”和后面的括号(带有可选内容)的字符串:

char c = Convert.ToChar(65535);
string format = @"/(?:[\0-" + c + @"]+)?B(?:[\0-" + c + @"]+)?\[(?:[\0-" + c + @"]+)?\]/";

This for strings that contain a "B" and brackets after it with numbers or nothing in it:这适用于包含“B”和后面带有数字或不包含任何内容的括号的字符串:

char c = Convert.ToChar(65535);
string format = @"/(?:[\0-" + c + @"]+)?B(?:[\0-" + c + @"]+)?\[(?:\d+)?\]/";

This for strings that contain a "B" and brackets after it with numbers in it:这对于包含“B”和后面带有数字的括号的字符串:

char c = Convert.ToChar(65535);
string format = @"/(?:[\0-" + c + @"]+)?B(?:[\0-" + c + @"]+)?\[(?:\d+)\]/";

This should match both examples you provided.这应该与您提供的两个示例相匹配。 You can check it and learn this format easily here: http://www.regexr.com/您可以在此处查看并轻松了解此格式: http : //www.regexr.com/

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

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