小编lia*_*m m的帖子

使用Regex OR运算符来容纳"A"或"An"的用户输入

我试图验证'a'|的用户输入 'an'将满足if语句.如果不满意,elif块将检查第二个单词是否为"about",如果不是"about",则它将检查"any".不幸的是,"约"和"任何人"都以字母'a'或'an'开头,因此我需要在'a'和'an'结束后添加'space'以允许Regex检测差异.

# Receive User input.
secrets = {}
secrets['text'] = request.GET.get('text')

regex_a = re.compile("(a|an)")
regex_about = re.compile('about')
regex_anyone = re.compile('anyone')

# Get second word from secrets[text]
secondword = secrets['text'].split()[1]
# If 2nd word is == 'a/an'
if regex_a.match(secondword):
    return HttpResponse("Text was (a) or (an)")

# Else if 2nd word is == about
elif regex_about.match(secondword):
    return HttpResponse("Second word was (about)")

elif regex_anyone.match(secondword):
    return HttpResponse("Second word was (anyone)")

else:
    return HttpResponse("Failed to interpret user input")
Run Code Online (Sandbox Code Playgroud)

即使用户输入"约"或"任何人"作为第二个单词,当前的正则表达式也会("(a|an)")返回Text was (a) …

python regex

4
推荐指数
1
解决办法
3506
查看次数

Express app.post被多次调用

我一直在构建一个youtube视频转换应用程序,该应用程序使用youtube-dl流式传输youtube视频并将其保存,直到我尝试流式传输一个小时以上的视频之前,一切工作正常。当任务完成的时间介于50%-100%之间或40-80秒之间时,就是重新运行整个代码块,导致同时发生多个流。因此,在等待管道完成时,永远不会发送响应。添加next(); 流功能外部允许转换完成,而不会中断代码块或重新运行它,但是在尝试发送响应时导致以下错误:

throw new Error('Can\'t set headers after they are sent.');

这是有问题的Node.js代码块:

app.post('/convert/', function (req, res, next){
  var url = 'http://www.youtube.com/'+req.body.url;
  var quality = req.body.quality;
  var socketId = req.body.socketId;


stream = youtubedl(url, 
  ['-f ' + quality],

  // // Additional options can be given for calling `child_process.execFile()`. 
  { cwd: __dirname });

stream.on('info', function(info) {

  console.log('Download started');
  console.log('filename: ' + info._filename);
  console.log('size: ' + info.size);
  console.log('format: ' + info.format);

  var fileName = info._filename;
  var videoId = info.id;
  var videoTitle = info.title;
  videoTitle …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express youtube-dl angularjs

4
推荐指数
1
解决办法
1411
查看次数

标签 统计

angularjs ×1

express ×1

javascript ×1

node.js ×1

python ×1

regex ×1

youtube-dl ×1