2014-10-19 157 views
-1

我有以下字符串红宝石正则表达式需要排除模式

ALEXANDRITE OVAL 5.1x7.9 GIA# 6167482443 FINE w:1.16 
ALEXANDRITE OVAL 4x6 FINE w:1.16 

我想匹配5.1和7.9以及4和6,而不是宽:1.16或W:1.16或者6167482443.到目前为止,我设法想出这些:

选配宽:1.16宽:1.16

([w][:]\d\.?\d*|[w][:]\s?\d\.?\d*) 

匹配其他数字:

\d+\.?\d{,3} 

我有点期待这不是因为{,3}而返回长序列,但它仍然有效。

我的问题是: 1.我如何将两种模式结合在一起,然后返回另一种模式? 2.如何排除长序列号码?为什么现在不被排除?

谢谢!

回答

2

你可以简单地使用下面的正则表达式。

\b(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?) 

DEMO

说明:

\b      the boundary between a word char (\w) and 
         something that is not a word char 
(      group and capture to \1: 
    \d+      digits (0-9) (1 or more times) 
    (?:      group, but do not capture (optional): 
    \.      '.' 
    \d+      digits (0-9) (1 or more times) 
)?      end of grouping 
)      end of \1 
x      'x' 
(      group and capture to \2: 
    \d+      digits (0-9) (1 or more times) 
    (?:      group, but do not capture (optional): 
    \.      '.' 
    \d+      digits (0-9) (1 or more times) 
)?      end of grouping 
)      end of \2