2017-01-15 25 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

相关问题