search

Home  >  Q&A  >  body text

python - What should I do if I want to remove the extra blank lines in the following code? How can I use regular expressions to achieve this?

/

52.html
152254.html
152255.html
152256.html
152257.html

8172396.html
8177822.html
8180348.html

PHP中文网PHP中文网2757 days ago685

reply all(2)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-18 10:55:03

    function delAmpty(str) {

     return str.replace(/\s/g,'');

    }

    alert(delAmpty('hello world'));

    This roughly means s matches the whitespace character, and replace is ok. Try it yourself

    Be sure to add /g when replacing everything!

    reply
    0
  • 迷茫

    迷茫2017-05-18 10:55:03

    My guess is to replace two newlines with one newline, also taking into account whether the blank lines contain spaces.

    The above string is equivalent to:

    a = '\\n\n52.html\n152254.html\n152255.html\n152256.html\n152257.html\n\n8172396.html\n8177822.html\n8180348.html';

    I don’t know python, it’s implemented in JavaScript:

    str.replace(/\n\s*\n/g,'\n');

    reply
    0
  • Cancelreply