0

What I want

I am trying to transform this:

foo awehorihawolvbahvierwba3485y089726y216
bar :aw]\e[;r\a32[5a94t8g-09po

into this:

foo,
bar

The problem

My current solution is to remove the "junk" and replace it with a comma:

$1

${1/(\\w+).*/$1,/gm}

This however leaves a trailing comma and I find it annoying to remove the trailing comma every time.

foo,
bar,

What I've tried

Take the original idea and nest it into this transform

${<insert transform here>/,$//}

... like this ...

${${1/(\\w+).*/$1,/gm}/,$//}

1 Answer 1

1

You appear to be triggering your snippet and then pasting your text into it, since you are only using $1 as your input. If that is not the case, let us know, it would probably be easy to modify this for a selection, etc.

This will work:

    "add a comma only if": {
        "prefix": ["py"],           // whatever prefix you want
        "body": [
            "${1/(\\w+).*(?=$(\r?\n)?)/$1${2:+,}/gm}"
            // "${1/(\\w+).*$(\r?\n)?/$1${2:+,\n}/gm}"  // this also works
        ]
    },

It will work for any number of input lines, putting a comma at the end of each except the last.

(?=$(\r?\n)?) positive lookahead for a newline, if so capture in group 2
${2:+,} if a group 2, add a comma

trailing comma demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is perfect :) I wish I could give 1000 upvotes to this for saving me a lot of time haha
Some day you will be able to ;>} I added a slight simplification above. You don't really need a positive lookahead if you put a newline into the conditional transform. About the same complexity really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.