[Python] How to match the first word of a line and separate subsequent values with a comma

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm stuck with a problem: I have over one hundred different markdown files and need to update a single line of frontmatter in them. In the tags: line, I need to wrap the values in brackets and separate them by a comma. There's hundreds of different values, so there's not really a pattern there.

    Here's an example:

    ---
    layout: post
    title: "Blog title"
    date: 2016-02-24 09:39:00 -0500
    tags: work career code programming
    layout: post
    ---


    And it needs to look something like:

    ---
    layout: post
    title: "Blog title"
    date: 2016-02-24 09:39:00 -0500
    tags: ["work", "career", "code", "programming"]
    layout: post
    ---


    I'm not sure the best way to do this. I've only been able to target the first value using regex with something like, (?<=tags: ).

    I can't figure out how to get the rest of the line and what to do with it from there.

    Continue reading...

Compartilhe esta Página