我不會撒謊,正則運算式絕對不是我的強項,所以希望借用它的人才。
我需要做的是從一個看起來像這樣的字串;
location = "Somewhere" and (Person IN (4,1)) and status in ("Closed")
我特別需要這部分;
(Person IN (4,1))
其中的部分(4,1)可以包括一對多的數字陣列。例如,如果我收到;
location = "Somewhere" and (Person IN (4,1,8,2,10,24,32,15,7)) and status in ("Closed")
那么我需要提取的部分是;
(Person IN (4,1,8,2,10,24,32,15,7))并且包含的??單詞不區分大小寫(例如;Person=person, IN=in)并且數字 [] 中的數字之間可能有空格,但字串和數字 [] 都將包含在其中(...),如圖所示可能是包含的字串前后的其他隨機內容。
一個可怕的嘗試可能是這樣的:/(?:Person in \()(?:([\/\s"](?:,?)))(?:\))?/gm但我知道這顯然不是我需要的,請建議并幫助正則運算式業余愛好者哈哈。
uj5u.com熱心網友回復:
顯然去regex是一個不錯的選擇。但是,有一種方法以實作這一目標substring和indexOf太。
var str = "location = \"Somewhere\" and (Person IN (4,1,8,2,10,24,32,15,7)) and status in (\"Closed\")"
let index1 = str.indexOf("(Person IN");
let index2 = str.substring(index1).indexOf(")");
console.log(str.substring(index1, index1 index2 2))
uj5u.com熱心網友回復:
您可以匹配左括號和右括號以及中間的逗號分隔數字,其中數字周圍可以有可選的空白字符。
\(Person in \(\s*\d \s*(?:,\s*\d \s*)*\)\)
\(Person in \(比賽(Person in (\s*\d \s*在可選空白字符之間匹配 1 位數字(?:,\s*\d \s*)*可選地重復匹配可選空格字符之間的逗號和 1 位數字\)\)比賽))
正則運算式演示
Javascript 中的一個例子
const regex = /\(Person in \(\s*\d \s*(?:,\s*\d \s*)*\)\)/gi;
const str = `location = "Somewhere" and (Person IN (4,1)) and status in ("Closed")
location = "Somewhere" and (Person IN ( 4, 1,8,2, 10,24 ,32,15, 7 )) and status in ("Closed")
location = "Somewhere" and (Person IN ()) and status in ("Closed")
location = "Somewhere" and (Person IN ( 4 )) and status in ("Closed")
`;
console.log(str.match(regex));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/396624.html
上一篇:SQL-REGEX如果其中包含@符號,則匹配整個單詞
下一篇:Eslint結合命名約定
