【问题标题】:Searcing for times and topics present in YouTube description搜索 YouTube 说明中的主题和主题
【发布时间】:2020-08-16 17:26:07
【问题描述】:

大约几周前,我开始学习正则表达式。为了练习,我想提取 youtube 视频的描述部分中存在的不同主题(以及它们各自的时间)(youtube 使用它来切片进度条,每个切片代表不同的主题)。 示例:对于https://www.youtube.com/watch?v=E4M_IQG0d9g,描述如下所示

00:00:00 - Introduction
00:00:15 - Machine Learning
00:01:15 - Supervised Learning
.
.
00:59:42 - scikit-learn
01:09:57 - Reinforcement Learning
By course's end, students emerge with experience in libraries for machine learning as well as knowledge of artificial intelligence principles that enable them to design intelligent systems of their own.

https://www.youtube.com/playlist?list...

***

This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

所以,首先我正在寻找一个正则表达式来直接执行此操作,但很快意识到提出一个执行此操作的单个正则表达式将非常困难,因为这些必须遵循没有严格的模式。

以下是我发现的一些其他格式:

0:00 Caden’s Hills - ‘Livin’ it Up’  <-------------
https://spoti.fi/3itjBFj
https://www.instagram.com/cadens_hill...
Download: https://apple.co/3adwC2U
2:39 ROMES - ‘Lose My Cool’          <-------------
https://spoti.fi/2XJY2Zp
https://www.instagram.com/romes/?hl=en
Download: https://apple.co/3affRV3

⌨️ (1:40:38) Sine and Cosine of Special Angles
⌨️ (1:48:41) Unit Circle Definition of Sine and Cosine
⌨️ (1:54:11) Properties of Trig Functions
⌨️ (1:04:50) Graphs of Sine and Cosine

incermenting numbers 15:30
decrementing numbers15:59 // this is not a mistake.. there is no space.
decimal numbers 16:23

01 - 00:00 - Into the sun (Sons Of The East)
02 - 03:48 - Looking too Closely (Fink)
03 - 07:20 - Numb/Encore (Linkin Park & Jay Z)

因此,正如您所见,想出一个正则表达式来准确地做到这一点是很困难的。所以我做了更多的思考和阅读。我想出了以下解决方案:

let input = `
some text I dont care about
...
lalalalla
mr white....
by order of hte foooking peaky blinders
destiny is all..

A blank stare grown on her face
She lost her soul and hope
She no longer feels the pain
She's having a love affair with dope
No more posey's an ring around the rosey
No more midnight rides to fly
Her new love Is jealous an clingy
Won't hesitate to let her die
With every intimate moment they have
With all there time shared together
Chasing her faded  youth to hell
The dope let death finally get her
----------------------------
// this is ehat I care about
Lecture 0:00

[First piece]
Elimination pivots and an example 

3:09
    - Failure of Elimination method 10:34
14:50

[Second piece]
Operations of matrices elimination 19:24
Row operations of Matrices Multiplication 20:22
Column operations of Matrices multiplication 21:43

[Third piece]
Elementary Matrix 24:33
    - 
    - Second step :  29:28
    - Over 32:10

Associative law in matrix  33:29
Permutation matrix 36:56
    - Row example: 37:36
 
Commutative law : Matrix order 41:17

Inverse matrix: 43:00


lalalalalalfdklfndklsfds
pinkman:i love science beach...
dfdskfndksfd
the end of the f**king world
`

let regex = /((?:\d{1,2})?(?::\d{1,2})?:\d{1,2})/; 
let arr = input.split(/[\r\n]+/); // split


let time =[];
let labels = [];
arr.forEach(e => {
  let match;
   if (match = regex.exec(e)){ // for all the elements of the array that matches this
  
  //get the number of characters before the match is found in the string
  beforeLength = match.index  
  // get the number of characters after the match
  afterLenght =  e.length - ( match.index + match[1].length )
  
  // I already have the match (time)  so put that into time array 
  time.push(match[1]);
  
  //here I am making this assumption: there is more probabily that label is present on the side that has more number of characters
  //(i also thought of randomly selecting 3 elements from the array and then select a side based on BEST of three results before this loop and use that side for labels for each element) unlike what I am doing right now.
  if(afterLenght > beforeLength){
    labels.push(e.substring(match.index + match[1].length).trim())
  }else {
    labels.push(e.substring(0,match.index).trim());
  }

   }
});

let result = labels
// just some cleanup
if(labels.every(s => s[0] == labels[0][0])) {
  result = labels.map(s => s.substring(1).trim());
}

console.log(time)
console.log(result)

我知道这并不完美,我意识到这会受到影响的一个问题是当输入中有类似下面的内容时

incermenting numbers 15:30
decrem2:03enting numbers 15:50 <------
decimal numbers 16:23

因此,如果标签本身存在类似的模式,那么正则表达式将停止搜索(因为我没有使用global 标志)并且我会得到错误的结果。

  • 那么,我怎样才能使这个解决方案更好?
  • 我怎样才能使它更健壮?
  • 如果你能想到一个更好的解决方案来解决这个问题,也请分享一下。我真的很想知道解决这个问题的其他方法

谢谢


还有一件事。

这是我正在使用的正则表达式:

/((?:\d{1,2})?(?::\d{1,2})?:\d{1,2})/;

这些是我希望与之匹配的字符串

:5           --> this is equal to 00:00:05
:45          --> 00:00:45
3:45
03:45
:03:45      
0:03:45
00:03:45

我还编写了一些将任何时间转换为 HH:MM:SS 的代码。所以,使用正则表达式我只是想找到这些模式,然后我可以进行转换。

【问题讨论】:

    标签: javascript regex string


    【解决方案1】:
    const re = String.raw`([-a-z :]*)([:\d]+)$`;
    const matches = input
      .match(new RegExp(re, 'gmi')) // we only want timestamped lines
      .map(match => match.match(new RegExp(re, 'i'))) // split into timestamp and string
      .map(([,str,time]) => ([time, str.trim()])); // only keep timestamp and string
    

    实时示例(但请查看浏览器的真实控制台以获得更漂亮的输出):

    'use strict';
    
    const input = `some text I dont care about
    ...
    lalalalla
    mr white....
    by order of hte foooking peaky blinders
    destiny is all..
    
    A blank stare grown on her face
    She lost her soul and hope
    She no longer feels the pain
    She's having a love affair with dope
    No more posey's an ring around the rosey
    No more midnight rides to fly
    Her new love Is jealous an clingy
    Won't hesitate to let her die
    With every intimate moment they have
    With all there time shared together
    Chasing her faded  youth to hell
    The dope let death finally get her
    ----------------------------
    // this is ehat I care about
    Lecture 0:00
    
    [First piece]
    Elimination pivots and an example 
    
    3:09
        - Failure of Elimination method 10:34
    14:50
    
    [Second piece]
    Operations of matrices elimination 19:24
    Row operations of Matrices Multiplication 20:22
    Column operations of Matrices multiplication 21:43
    
    [Third piece]
    Elementary Matrix 24:33
        - 
        - Second step :  29:28
        - Over 32:10
    
    Associative law in matrix  33:29
    Permutation matrix 36:56
        - Row example: 37:36
     
    Commutative law : Matrix order 41:17
    
    Inverse matrix: 43:00
    
    
    lalalalalalfdklfndklsfds
    pinkman:i love science beach...
    dfdskfndksfd
    the end of the f**king world
    `;
    
    const re = String.raw`([-a-z :]*)([:\d]+)$`;
    const matches = input
      .match(new RegExp(re, 'gmi'))
      .map(match => match.match(new RegExp(re, 'i')))
      .map(([,str,time]) => ([time, str.trim()]));
    
    console.log(matches);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 1970-01-01
      • 2019-04-09
      • 2019-07-06
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多