Receipt of part of the line found through regular expressions



  • I can't write a regular text. I will not consider other methods of searching the text. Because I want to study this method. Text:

    Пользователь: ФИО - Иванов Иван Иванович
    организация - ОАО "Мороженка"
    здание - 111
    комната - 111
    телефон - 1-11-11
    Другое
    ПК № - 4550
    на улице: 111,111,111.
    Дата и время - 11:22 12.12.2012.
    

    We need to find the PC number.4550) I kind of found a line. ПК № - 1017 regular

    /(ПК № - )[0-9]{1,11}/g
    

    I can't get the PC number myself.



  • The fascinating underwear should be reduced to:

    /ПК № - ([0-9]{1,11})/g
            ^           ^
    

    I think the number of numbers after ПК № - You don't have to limit 11. May and + Use. But it's up to you.

    I suggest that code:

    var re = /ПК\s+№\s+-\s+([0-9]+)/g; 
    var str = 'Пользователь: ФИО - Иванов Иван Иванович\nорганизация - ОАО "Мороженка"\nздание - 111\nкомната - 111\nтелефон - 1-11-11\nДругое\nПК № - 4550\nна улице: 111,111,111.\nДата и время - 11:22 12.12.2012.';
    var m;
    

    while ((m = re.exec(str)) !== null) {
    document.getElementById("r").innerHTML += m[1]; // текст, захваченный подмаской №1
    }

    <div id="r"/>

    Regular expression https://regex101.com/r/jC3lL4/1 😞

    • ПК\s+№\s+-\s+ - Here. \s+ Any gaps, 1 and more
    • ([0-9]+) - 1 and more numerals are placed in captive No1.

    If gaps are not necessary, i.e. if they may not be substituted \s+\s* Quantifier * means 0 or more




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2