2

I am currently working on some Python snippets for VSCode using JavaScript regular expressions and I'd like to have one like :

Given

var1, var2, var3

Produce

self.var1 = var1
self.var2 = var2
self.var3 = var3

I currently know how to replace commas with line jump but not how to replace each word with another string. I'm pretty bad at regular expressions so it doesn't help. I think I'll need something that does the following :

var1, var2 -> self.var1 = var1, self.var2 = var2

Thanks for your help

Edit : From suggestions in comments I tried this : /(.*,)/self.$0 = $0/ It obviously doesn't work for several reasons : it matches all strings up to the last comma. VSCode doesn't give access to "\w" but i don't see how it would help. Other thing I tested that couldn't work either since I'm looking for duplicating each of the words : /(.*)/self.$0 = $0/

And since I didn't post it, here is the comme replacement by a new line : /[,]/\n/g

Edit2 (updated) : From comment suggestions I tried : /(.*?)(?:, |$)/self.$1 = $1\n/g which leads to almost perfect result :

toto, titi, tata

leads to

self.toto = toto
self.titi = titi
self.tata = tata
self. = 
17
  • 1
    I actually don't know how to try anything. As I said, I can replace commas with line jump (which is quite easy) but I don't know where to start. I'd like to select the beginning of each word and replace it with "self." as well as duplicating each word and appending an "=" before. But I don't know how to do it Commented Mar 10, 2020 at 14:52
  • 1
    Ok, so this doesn't help at all since I already went through the entire regex doc of JS and Python. I want ti duplicate a word and insert string before/after it. None of the thing I read answers this nor leads to an idea. Commented Mar 10, 2020 at 15:08
  • 1
    Ok so it was not clear at all since you linked something to add string at the end of a line. I tested what you explained but it does not work since I am matching any word, just as you said. But what I want is to duplicate each of the words : "toto, tata, titi" should give "toto toto, tata tata, titi titi" and it gives "toto, tata, titi toto, tata, titi" Commented Mar 10, 2020 at 15:16
  • 1
    Try the following: /(.*?)(?:, |$)/self.$0 = $0\n/ Commented Mar 10, 2020 at 15:26
  • 1
    I think we have something here, thanks @HarmvanderWal. Sadly it doesn't exactly work. I'll update the question Commented Mar 10, 2020 at 15:31

2 Answers 2

3

Add the following to your python.json snippet file

  "var-init": {
    "prefix": "varinit",
    "body": [
      "${1/([\\w_]+)(?:[,\\s]|$)/self.$1 = $1\n/g}",
      "$0"
    ],
    "description": "var initialisation"
  }
  • Type the prefix: varinit
  • Press Tab
  • Type the variables you want separated by , with or without spaces: titi,tata,toto
  • Press Tab

Result

self.titi = titi
self.tata = tata
self.toto = toto

And the cursor on the next line.

Edit

A simplification of the regex and using the tip from Mark about the if-replacement ${2:+\n}

"${1/([\\w_]+)([, ])*/self.$1 = $1${2:+\n}/g}"

now the variables can be separated with spaces or ,

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

2 Comments

Looks familiar... I would suggest ` "${1/\\s*([^,]+)(,)*/self.$1 = $1${2:+\n}/g}",` as the regex is a little simpler and it doesn't add an extra newline after the last variable. And the spaces are handled much better. I get spaces added to the beginning of subsequent lines with your code.
It is also easy to modify this to where the variables are already listed and selected. If that is a use case for the OP. For more complicated cases of using variable numbers of arguments in a class constructor snippet see stackoverflow.com/questions/53998252/… and for a class initializer see stackoverflow.com/questions/58225204/…
0

You may use

Find What: \s*([^,\s]+)(?:,|$)
Replace With: self.$1 = $1\n

See the regex demo.

Details

  • \s* - 0 or more whitespaces
  • ([^,\s]+) - capturing group 1: one or more chars other than whitespace and comma
  • (?:,|$) - a non-capturing group matching , or end of line.

enter image description here

Comments

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.