简体   繁体   English

帮助制作好的正则表达式?

[英]Help to make a good Regex?

Can anyone help me make a regex or give me a good solution that can split/check the following string: 谁能帮我做一个正则表达式或给我一个好的解决方案,可以拆分/检查以下字符串:

"<2342Flsdn3Z><9124Fsflj20>" “ <2342Flsdn3Z> <9124Fsflj20>”

Everything starts with a "<" and the 6 caracter is a "F" and the string ends with a ">" Is it possible to make a Regex that can find "strings" like this? 一切都以“ <”开头,而6个字符是“ F”,字符串以“>”结尾。是否可以使正则表达式能够像这样找到“字符串”?

How about this: <.{4}F[^>]+> 怎么样: <.{4}F[^>]+>

It matches the opening < , followed by any 4 chars, F, then anything till the closing > (by matching anything that is not a > ). 它与开头<匹配,后跟任意4个字符F,然后匹配直到结尾> (通过匹配不是>任何内容)。

string input = "<2342Flsdn3Z><9124Fsflj20>";
string pattern = "<.{4}F[^>]+>";
foreach (Match m in Regex.Matches(input, pattern))
{
    Console.WriteLine(m.Value);
}

EDIT: part of making a good regex is clearly specifying the pattern you want to match. 编辑:制作一个好的正则表达式的一部分是明确指定要匹配的模式。 For example, the way you worded the question leaves certain details out. 例如,您对问题的措辞方式忽略了某些细节。 I responded with my pattern to match any character as long as F was where you specified. 只要F是您指定的位置,我就会用我的模式进行响应以匹配任何字符。

For a better regex you could've told us a number of things: 为了获得更好的正则表达式,您可以告诉我们很多事情:

  • Chars before F will always be digits and of length 4: \\d{4} or [0-9]{4} F之前的字符始终是数字,长度为4: \\d{4}[0-9]{4}
  • Chars after F will be of X length (6?) and can only be numbers and letters: [\\dA-Z]{6} F之后的字符为X长度(6?),并且只能为数字和字母: [\\dA-Z]{6}
  • Case is insensitive: use RegexOptions.IgnoreCase (.NET) or use [a-zA-Z] 大小写不敏感:请使用RegexOptions.IgnoreCase (.NET)或使用[a-zA-Z]
  • State your intention: are you matching it? 说明您的意图:与之匹配吗? Trying to extract the inner value? 试图提取内在价值? What do you mean by split? 分裂是什么意思? Split on what? 分裂什么?
  • Specify the language you're using: C#, Python, Perl, etc. (you did this one) 指定您使用的语言:C#,Python,Perl等(您已完成此操作)

Yes. 是。 <[A-Za-z\\d]{4}F[A-Za-z\\d]{6}>

< followed by Any 4 letters or digits followed by F followed by any 6 letters or digits follow by > <,后跟任意4个字母或数字,然后按F,再后跟任意6个字母或数字,然后再按>

I made the assumption its always six after F. You can modify the repetition to suit your needs. 我的假设始终是F之后的六个。您可以根据自己的需要修改重复次数。

Original proposed solution to conserve valid comment so others can learn from my mistake: [\\d\\w]{4}F[\\d\\w]{6}> 最初提出的解决方案可以保留有效的注释,以便其他人可以从我的错误中学习: [\\d\\w]{4}F[\\d\\w]{6}>

easy: 简单:

<\d{4}F\w+>

Or, to just get the strings: 或者,仅获取字符串:

(?<=<)\d{4}F\w+(?=>)

我做出一些假设,即括号内的所有内容都必须是文字字符,并且在尾括号之前至少应有一个,但可以是任意数量的文字字符。

var regex = new Regex( "<\w{4}F\w+>" );

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

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