我有一个这样的字符串:
import multiprocessing as mp
def func(val):
print "\nInside Function.....\n"
return val*val
if __name__ == '__main__':
cpu_count = mp.cpu_count()
pool = mp.Pool(processes = cpu_count)
results = []
num = 1
while cpu_count >= 1:
results.append(pool.apply_async(func, (num,)))
cpu_count = cpu_count - 1
num = num + 1
output = [p.get() for p in results]
print output
pool.close()
pool.join()
现在我想要这个:
var str = "this is [link1][1]
this is [link2][2]
this is [link3][3]
this is [link4][4]
[1]: http://example1.com
[2]: http://example2.com
[3]: http://example1.com
[4]: http://example4.com";
正如您在上面的示例中所看到的,有两件事:
var str = "this is [link1][1]
this is [link2][2]
this is [link3][1]
this is [link4][4]
[1]: http://example1.com
[2]: http://example2.com
[4]: http://example4.com";
\n
并将[3]: http://example1.com
替换为{{1} })有一个正则表达式将[3]
及其[1]
分为两组:
[any digit]:
在url
中,正则表达式匹配:
/(\[[0-9]*]:)\s*(.*)\r?\n?/gm
另外还有另一个正则表达式可以删除仅链接之间的所有浪费str
:
//group1
$1: [1]:
[2]:
[3]:
[4]:
//group2
$2: http://example1.com
http://example2.com
http://example1.com
http://example4.com
嗯,我怎么能这样做?
答案 0 :(得分:1)
这可能是解决问题的方法:
function removeMultipleMatkdownLinks(markownString) {
var seperateLinks = /(\[[0-9]*]):\s*(.*)\r?\n?/gm;
var removeNewLines = /(^\[[0-9]*]:.*)\n+/gm;
var result;
var urls = [], ids = [];
var formattedString = str;
while ((result = seperateLinks.exec(str)) !== null) {
if (result.index === seperateLinks.lastIndex) {
result.lastIndex++;
}
//check if link already exists
var index = urls.indexOf(result[2]);
if(index < 0) {
urls.push(result[2]);
ids.push(result[1]);
} else { //remove links and replace ids
var removeLink = new RegExp("(\\" + result[1] + ":.*\\r?\\n?)", "gm"); ///(\[1\]:.*\n)/gm
var changeNumber = new RegExp("(\\" + result[1] + ")", "gm");
formattedString = formattedString
.replace(removeLink, "")
.replace(changeNumber, ids[index]);
}
}
return formattedString.replace(removeNewLines, "$1\n");
}