2014-10-19 159 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