需要正则表达式帮助。 [英] Regex help required.

查看:87
本文介绍了需要正则表达式帮助。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些表达式的帮助。

应该匹配的示例文本:

TEXT 4 B 999.9999 999.9999 TEXT1 TEXT2 TEXT3 TEXT4

组匹配说明和表达式匹配:

组1:TEXT =字母数字字符:(\ w +)
组2:4 =单个数字值1-6:([1-6])
组3:B =单个字母B(输入可选):( B)? ?
第4组:999.9999 =十进制数1-999.9999:(\d {1,3} \.\d {0,4} | \d {1,3} \。| \ d {1,3})
第5组:999.9999 =与上述相同
第6组:TEXT1 TEXT2 ... = 0至6个字母数字字。应该受到第2组的限制。如果第2组是2然后允许0到2个单词,如果6允许0到6个单词(甚至可能吗?):(不知道)

所以我的表达如此远是:

(\ w +)([1-6])(B)? ?(\d {1,3} \.\\\ {0,4} | \d {1,3} \。| \d {1,3})(\d {1, 3} \. \\\ {0,4} | \d {1,3} \。| \d {1,3})(这里有什么?)

每个单独的空格分开group。

我写什么表达式来获得第6组,是否可以对组2进行检查。另外,我的表达式可以改进我的小数吗?

谢谢

I need some help with an expression.

Sample text that should match:

TEXT 4 B 999.9999 999.9999 TEXT1 TEXT2 TEXT3 TEXT4

Group explanation and expression to match:

Group 1: TEXT = alphanumeric characters  : (\w+)
Group 2: 4 = single digit value 1-6 : ([1-6])
Group 3: B = single letter B (input optional) : (B)? ?
Group 4: 999.9999 = decimal number 1-999.9999 : (\d{1,3}\.\d{0,4}|\d{1,3}\.|\d{1,3})
Group 5: 999.9999 = same as above
Group 6 :TEXT1 TEXT2... = from 0 to 6 alphanumeric words. Should be limited by group 2. If group 2 is 2 then allow 0 to 2 words, if 6 allow 0 to 6 words (is that even possible?) : (don't know)

So my expression so far is:

(\w+) ([1-6]) (B)? ?(\d{1,3}\.\d{0,4}|\d{1,3}\.|\d{1,3}) (\d{1,3}\.\d{0,4}|\d{1,3}\.|\d{1,3}) (what here?)

Single spaces separate each group.

What expression do I write to get group 6 and is it possible to have it checked against group 2. Also, can my expression for matching my decimal be improved?

Thanks

推荐答案

匹配之外正则表达式的最终目的是什么?它是提取数据还是这个验证?

如果是提取,甚至是验证,为什么不将它标记化并让消费代码正确地确定项目,例如:

([^ \ s] +)

那将给出每一个项目,然后代码可以通过列号确定它是什么项目...


What is the eventual purpose of the regex outside of the match? Is it to extract the data or is this validation?

If it is extraction, or even validation, why not just tokenize it and have the consuming code properly determine the items such as:

([^\s]+)

That will give every single item and then the code can determine via column number what item it is...



这篇关于需要正则表达式帮助。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆