拆分过去是一个列表的字符串 [英] Split string that used to be a list

查看:170
本文介绍了拆分过去是一个列表的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

  var lines = this.result.split('\\\
');
for(var line = 0; line< lines.length; line ++){
console.log(lines [line]);
var sublines = lines [line] .split('');
for(var subline = 0; subline< sublines.length; subline ++){
console.log(sublines [subline]);
}
}

我希望它能从中提取令牌该字符串,然后解析每个标记的整数,但它看起来像 split()不是在这里工作的!



这是我看到的:


<0>(0,(u'5643145391',u'11367866245')) p>

获得两次安慰,这应该意味着没有完成拆分。在我的真实数据中,大数字列表的长度是150,但这应该不重要。



如何拆分(部分unicode)字符串? p>




jsFiddle 重现该问题。然而,似乎小提琴只打印一次字符串,而没有注释该信息打印多少次。






所需输出将会每次迭代一行:

  0 
5643145391
11367866245

但是接近这个的东西值得赞赏。

解决方案

这似乎是 RegExp 可能对您有用,

 (//开始捕获组
- ?//匹配0或1减号
\ {1,} //匹配1到无限位
)//结束捕获组

  var line =(0,(u'5643145391',u 11367866245')),regex = /( - ?\d {1,})/ g; console.log(line.match(regex));  


I have this code:

var lines = this.result.split('\n');
for (var line = 0; line < lines.length; line++) {
    console.log(lines[line]);
    var sublines = lines[line].split(' ');
    for (var subline = 0; subline < sublines.length; subline++) {
        console.log(sublines[subline]);
    }
}

which I would hope that it would extract the tokens from the string and then parse the integer from every token, but it seems like split(" ") is not what will work here!

Here is what I see:

(0, (u'5643145391', u'11367866245'))

getting consoled twice, which should mean that no split is done. In my real data the list with the big numbers is 150 in length, but that shouldn't matter.

How to to split that (partially unicode) string?


jsFiddle to reproduce the issue. However it seems that the fiddle prints the strings only once, without annotating how many times this message printed.


Desired output would be, a line per iteration:

0
5643145391
11367866245

but anything close to this would be appreciated.

解决方案

This seems like something that a RegExp could be useful for,

(                              // begin capture group
  -?                           // match 0 or 1 minus sign
  \d{1,}                       // match 1 to unlimited digits
)                              // end capture group

var line = "(0, (u'5643145391', u'11367866245'))", 
    regex = /(-?\d{1,})/g;

console.log( line.match(regex) );

这篇关于拆分过去是一个列表的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆