2017-01-15 24 views
0

我有一個像下面算括號內的數字字符串中的

*WTY: but the light of the dining room suddenly turn off . 
%mor: conj|but det|the n|light prep|of det|the adj|dining n|room adv|suddenly v|turn adv|off . 
%snd: <00:14:74><00:25:53> 
%WTY: {and} rpl but {suddenly} rpl (0.43) {the} * (1.07) {the light (0.78) suddenly turn off and} # (1.24) the light of the dining room suddenly turn off . err_m_s err_m_l ::: | 

我想提取圓括號內的數字(),並計算這些數字的總和一個特定的字符串。 我已經嘗試過以下RegEx提取數字,但它沒有返回任何結果。

str.match(/\((\d+)\)/) 

回答

2

你可以試試這個:

/\((\d*\.?\d*)\)/g 

Explanation

const regex = /\((\d*\.?\d*)\)/g; 
 
const str = `*WTY: but the light of the dining room suddenly turn off . 
 
%mor: conj|but det|the n|light prep|of det|the adj|dining n|room adv|suddenly v|turn adv|off . 
 
%snd: <00:14:74><00:25:53> 
 
%WTY: {and} rpl but {suddenly} rpl (0.43) {the} * (1.07) {the light (0.78) suddenly turn off and} # (1.24) the light of the dining room suddenly turn off . err_m_s err_m_l ::: |`; 
 
let m; 
 

 
var val=0.0; 
 
while ((m = regex.exec(str)) !== null) { 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    //console.log(m[1]); 
 
    val+=parseFloat(m[1]); 
 
} 
 
console.log(val);

爲了彌補您的評論在接受的答案

還有一件事情,如果我只需要計算在::(包括:;:a,:;:b,:;:a)之前或之後的括號 的和即可。

你可以應用這個表達式:

:;:\s*\((\d*\.?\d*)\)|\((\d*\.?\d*)\)\s*:;: 

const regex = /:;:\s*\((\d*\.?\d*)\)|\((\d*\.?\d*)\)\s*:;:/g; 
 
const str = `*WTY: but the light of the dining room suddenly turn off . 
 
%mor: conj|but det|the n|light prep|of det|the adj|dining n|room adv|suddenly v|turn adv|off . 
 
%snd: <00:14:74><00:25:53> 
 
%WTY: {and} rpl but {suddenly} rpl :;: (0.43) {the} * (1.07) :;: {the light (0.78) suddenly turn off and} # (1.24) the light of the dining room suddenly turn off . err_m_s err_m_l ::: |`; 
 
let m; 
 

 
var val=0.0; 
 
while ((m = regex.exec(str)) !== null) { 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    if(typeof m[1] !== 'undefined') 
 
    val+=parseFloat(m[1]); 
 
    else 
 
    val+=parseFloat(m[2]); 
 
    //val+=parseFloat(m[1]); 
 
} 
 
console.log(val);

+0

謝謝噓。你是一個拯救生命的人:) –

2

你有沒有結果的主要原因是,你不佔數量的點,所以你會錯過所有的非整數。一旦你改正了,你仍然只會得到一個結果,因爲你沒有在你的正則表達式中指定全局修飾符(g)。

您可以使用此三步轉換:

const sum = s.match(/\([\d.]+(?=\))/g) // get the numbers 
      .map(a => +a.substr(1)) // remove opening parenthesis and convert to number 
      .reduce((a,b) => a+b); // total them 

演示:

const s = `*WTY: but the light of the dining room suddenly turn off . 
 
%mor: conj|but det|the n|light prep|of det|the adj|dining n|room adv|suddenly v|turn adv|off . 
 
%snd: <00:14:74><00:25:53> 
 
%WTY: {and} rpl but {suddenly} rpl (0.43) {the} * (1.07) {the light (0.78) suddenly turn off and} # (1.24) the light of the dining room suddenly turn off . err_m_s err_m_l ::: |`; 
 

 
const sum = s.match(/\([\d.]+(?=\))/g) // get the numbers 
 
      .map(a => +a.substr(1)) // remove opening parenthesis and convert to number 
 
      .reduce((a,b) => a+b); // total them 
 

 
console.log(sum.toFixed(2));

注:調用.toFixed是可選的,但它解決了一個問題,你可能會得到floating point inaccuracies

+0

非常感謝你。這工作。還有一件事情,如果我只需要在::(包括:;:a::;:b,:;:a)之前或之後只計算括號的總數。例如看到這個小提琴https://jsfiddle.net/y49vgwdz/ –

+0

這真的值得一個新的問題。它還需要更多的規範,比如你是否在*'';:'之間尋找數字*,並且/或者':;:'是否同時對*和*之前的值產生影響,如果':;'後面跟着'c'或'd'或'è',......正如你所看到的,還有很多需要解釋的地方:最好有清晰的規格,你的嘗試(重要!)一個新的問題。 – trincot

相關問題