我应该如何使用正则表达式来搜索和替换网址中的特殊字符?

时间:2018-06-12 15:48:27

标签: regex python-2.7

假设我有一个网址=“https://google.com/?q=cats”,那我该如何搜索“://”和“?”并用空格替换它们?

// CBOW negative sampling gradient calculations  
f = 0;
l2 = target * layer1_size;
for (c = 0; c < layer1_size; c++) f += neu1[c] * syn1neg[c + l2];
// ** here, we still update, but essentially round the value to 1 or 0
if (f > MAX_EXP) g = (label - 1) * alpha;
else if (f < -MAX_EXP) g = (label - 0) * alpha;
else g = (label - expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]) * alpha;

// ---------------------------

// Skipgram hierarchical softmax gradient calculations
f = 0;
l2 = vocab[word].point[d] * layer1_size;
for (c = 0; c < layer1_size; c++) f += syn0[c + l1] * syn1[c + l2];
// ** here, we don't update if f is outside the range given by MAX_EXP **
if (f <= -MAX_EXP) continue;
else if (f >= MAX_EXP) continue;
else f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
g = (1 - vocab[word].code[d] - f) * alpha;

2 个答案:

答案 0 :(得分:2)

>>> re.sub(r'(://|\?)', ' ', url)
'https google.com/ q=cats'

答案 1 :(得分:2)

这应该可以解决你的问题:

re.sub('://|\?', ' ', url)
#https google.com/ q=cats