2016-07-08 55 views
0

我試圖捕獲這個正則表達式右側沒有捕獲到左側的單詞。捕捉|右側的詞(或)在正則表達式不是在左邊

在下面的代碼,左側捕獲「17英寸」在此字符串:「這235/45R17的17寸輪胎」

(?<=([-.0-9]+(\s)(inches|inch)))|??????? 

然而,什麼我把在右側,這樣的作爲一個簡單的+ w是干擾左側

我如何告訴RegEx捕獲任何單詞,除非它是一個數字後面英寸 - 在這種情況下捕獲17和英寸?

+0

,什麼是與Elasticsearch連接?你想用Elasticsearch和那個正則表達式來做什麼? –

+0

謝謝,我正在構建一個標記器來基本上在特定的部分中分割一個字符串。無論是在簡單的空間上,還是數字和空間的組合(5英寸) – hitwill

回答

1

說明

((?:(?![0-9.-]+\s*inch(?:es)?).)+)|([0-9.-]+\s*inch(?:es)?) 

Regular expression visualization

**要看到圖像更好,只需右鍵點擊新窗口

現場演示的圖像,然後選擇視圖

https://regex101.com/r/fY9jU5/2

示例文本

this 235/45R17 is a 17 inch tyre 

樣品匹配

  • 捕獲組1將是不匹配的17 inch
  • 捕獲組2將值是inche的數量小號
MATCH 1 
1. [0-20] `this 235/45R17 is a ` 

MATCH 2 
2. [20-27] `17 inch` 

MATCH 3 
1. [27-32] ` tyre` 

說明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (1 or more 
          times (matching the most amount 
          possible)): 
---------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     [0-9.-]+     any character of: '0' to '9', '.', 
           '-' (1 or more times (matching the 
           most amount possible)) 
---------------------------------------------------------------------- 
     \s*      whitespace (\n, \r, \t, \f, and " ") 
           (0 or more times (matching the most 
           amount possible)) 
---------------------------------------------------------------------- 
     inch      'inch' 
---------------------------------------------------------------------- 
     (?:      group, but do not capture (optional 
           (matching the most amount 
           possible)): 
---------------------------------------------------------------------- 
      es      'es' 
---------------------------------------------------------------------- 
     )?      end of grouping 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
     .      any character except \n 
---------------------------------------------------------------------- 
    )+      end of grouping 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
|      OR 
---------------------------------------------------------------------- 
    (      group and capture to \2: 
---------------------------------------------------------------------- 
    [0-9.-]+     any character of: '0' to '9', '.', '-' 
          (1 or more times (matching the most 
          amount possible)) 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 
          or more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    inch      'inch' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
     es      'es' 
---------------------------------------------------------------------- 
    )?      end of grouping 
---------------------------------------------------------------------- 
)      end of \2 
---------------------------------------------------------------------- 
+0

非常感謝 - 這絕對能讓我走上正確的道路。我得到的最終字符串是:(?<=([ - 。0-9] +(\ s)(inch)))|(?<!([ - 。0-9]))\ s +(?!= (英寸))如果前綴和後綴缺失,則右側匹配 – hitwill

+0

如果您滿意,請將答案標記爲已接受。 –

0

它更容易,更安全的沒什麼只是先更換所有不想要的東西。
只有匹配你正在尋找的東西。

例如在此JavaScript例如:

var str = "this 235/45R17 is a 17 inch tyre of more than 9 inches."; 
var result = str.replace(/\s[\d.\-]+\sinch(?:es)?/gi, "").match(/\-?\d+\.?\d*/gi); 

爲了得到結果235,45,17

負先行是可能的,但最好使用詞語邊界\灣
爲了避免像仍然匹配數字中不應該匹配的第一個數字的問題。

例如:

var result = str.match(/(?:\-?\d+\.?\d*)(?:[a-z]|\b)(?!\s+inch(?:es)?)/gi); 

爲了讓結果235,45R,17