To create a snippet in vscode that returns the name of a file I use:
{$TM_FILENAME}
To create a snippet in vscode that returns the name of a directory with its first capital letter I use:
${TM_DIRECTORY/.*\\/(.*)$/${1:/capitalize}/g}
But I need to get a subdirectory and leave all the letters in lowercase. For example,
a/b/c/d/e
how could i get the \d directory?
You can use
"LowercaseFolderPath": {
"scope": "",
"prefix": "lowercasefolderpath",
"body": [
"${TM_DIRECTORY/([^\\/\\\\]+)(?=[\\/\\\\][^\\/\\\\]*$)/${1:/downcase}/}"
],
"description": "Lower-case folder path"
},
Here, ([^\/\\]+)(?=[\/\\][^\/\\]*$) regex captures the last but one subdirectory into $1, and the ${1:/downcase} replacement turns it to lower case.
See the regex demo.
Related
Background:
I am adding a custom JavaScript snippet in VS Code to insert the file path of the current file. VS Code provides variables to get the file path but the file path contains backslashes in the path. And I want to get the path with '/' instead of '\'
eg. 'hello\world.js' -> 'hello/world.js'
VS Code also provides variable transformations using regex. I tried to replace backslashes with forward slashes but I could not make it work. I also checked similar questions but I could not any result for this specific to variable transformations in VS Code snippets. And I could not figure it out with other similar solutions as I'm new to regex.
What I tried:
This works fine and replaces backslashes '\' with '_'
"filepath": {
"prefix": "filepath",
"body": ["/${RELATIVE_FILEPATH/([\\\\])/_/g}"],
"description": "Path of current file"
}
But if I change '_' with '/' it does not work.
"filepath": {
"prefix": "filepath",
"body": ["/${RELATIVE_FILEPATH/([\\\\])///g}"],
"description": "Path of current file"
}
I tried some variations by escaping slash and using regex groups but I could not make it work.
\\\\/\\//g is your expression. That one should mean the same, because it is a choice of only one character: [\\\\]/\\//g.
I'm coding in Elixir/Phoenix Framework using VS Code and trying to transform the following relative path
lib/shop_web/live/product_live/index.ex
into
ShopWeb.Live.ProductLive.Index
using snippets.
The closest to that was the regex below
"${RELATIVE_FILEPATH/^(lib\\/|test\\/)(\\w)|(.ex|.exs)$|\\/(\\w)|_(\\w)/${2:/upcase}${4:/upcase}${5:/upcase}/g}"
who gives me the following output
ShopWebLiveProductLiveIndex
I could not find a way to insert the missing dots.
Can anyone help me?
Thanks in advance!
Try this:
"test7": {
"prefix": "al",
"body": [
// my version
"${RELATIVE_FILEPATH/^([^\\/\\\\]+[\\/\\\\])|(\\.ex|\\.exs)$|([^._\\/\\\\]+)|_|([\\/\\\\])/${3:/capitalize}${4:+.}/g}",
// your version tweaked
"${RELATIVE_FILEPATH/^(lib[\\/\\\\]|test[\\/\\\\])(\\w)|(\\.ex|\\.exs)$|([\\/\\\\])(\\w)|_(\\w)/${2:/upcase}${4:+.}${5:/upcase}${6:/upcase}/g}",
],
"description": "alert line"
}
[Note: I made these work for both path.separators / and \. If you don't need that you could shorten the snippet by a lot.]
Your version was very close. I changed it to \\.ex just to make the dots explicit.
I also added a 4th capturing group ([\\/\\\\]) just before the 5th as in ([\\/\\\\])(\\w).
Now that 4th group can be used in a conditional ${4:+.} to add the .'s where the path separators were.
My version is a little shorter - it matches but doesn't use whatever directory is first, be it lib or test or whatever. If that doesn't work for you it is easy to modify that bit of the regexp. I shortened it to 4 capture groups.
([^._\\/\\\\]+)|_|([\\/\\\\]) the end of my version:
([^._\\/\\\\]+) : match characters other than ._\/, or
_ : match it but we aren't using it so no need for a capture group, or
([\\/\\\\]) : match just the path separator in group 4 to use in the conditional.
${4:+.} : conditional, if there is a group 4 (a path separator) add a ..
Thanks to #Mark, my snippet to create a module in Elixir or Phoenix Framework looks like this now:
"Module": {
"prefix": "defmodule",
"description": "Create a module by the Elixir naming convention",
"body": [
"defmodule ${RELATIVE_FILEPATH/^([^\\/\\\\]+[\\/\\\\])|(\\.ex|\\.exs)$|([^._\\/\\\\]+)|_|([\\/\\\\])/${3:/capitalize}${4:+.}/g} do",
"\t$1",
"end"
],
}
As the naming convention, the output for the file in my question lib/shop_web/live/product_live/index.ex will be:
defmodule ShopWeb.Live.ProductLive.Index do
end
So i've got a regex in vscode that matches any sentence after the first {space} using (?<=\s).* per line.
Here's a screenshot of what the regex is matching.
I'm trying to convert a txt file consisting of over 50,000 lines of un-formatted objects to correct json by placing all the text elements inside strings. So i'm using the find and replace feature to first match the text elements and then individually place them inside "" using the replace module in vscode.
However when I use $0 in the replace module. It seems the $0 strangely it captures all words past the second found space, rather then the first found space.
You see the issue? Everything looks to be replacing accordingly until you look here.
This occurs for any sentences that are actually spaced out rather then connecting with -
I have no clue why this is the case. My guess is I need to reformat my regex somehow inside (), because to my knowledge, the $ method of calling regex expressions declared in the find module only captures the regex expressions between the defined brackets?
Here's a sample of the txt document.
"Type": language
"Subtag": ht
"Description": Haitian
"Description": Haitian Creole
"Added": 2005-10-16
"Suppress": Latn
%%
"Type": language
"Subtag": hu
"Description": Hungarian
"Added": 2005-10-16
"Suppress": Latn
%%
"Type": language
"Subtag": hy
"Description": Armenian
"Added": 2005-10-16
"Suppress": Armn
"Comments": see also hyw
%%
"Type": language
"Subtag": hz
"Description": Herero
"Added": 2005-10-16
%%
"Type": language
"Subtag": ia
"Description": Interlingua (International Auxiliary Language Association)
"Added": 2005-10-16
%%
"Type": language
"Subtag": id
"Description": Indonesian
"Added": 2005-10-16
"Suppress": Latn
"Macrolanguage": ms
%%
"Type": language
"Subtag": ie
"Description": Interlingue
"Description": Occidental
"Added": 2005-10-16
%%
"Type": language
"Subtag": ig
"Description": Igbo
"Added": 2005-10-16
I'm sure this is quite a specific and uncommon issue, so thanks in advance.
Update
I forgot to mention I'm using the javascript regex flavor.
Past solution seemed to only work for PHP and not javascript.
I tested it with the following regex:
^([^:]*:\s)(.*)
and with the following replace sting:
$1"$2"
It seems to do the job.
This seems to work better with Javascript:
([^:]*:\s)(.*)
Same replace string.
Result:
I don't have VSCode installed at the moment either, but does this work? On the first capture group?
^.*?\s(.*$)
Can try installing VSCode if not.
Removing the ^ and $ may affect results also (start, end line matches in single versus multiline modes).
I'm trying to create the following line as a snippet for VS Code:
MyFooVariable mytype `json:"myFooVariable"`
So I have the following snippet base
"Struct member declaration with json decorator": {
"prefix": "json",
"body": [
"${1} ${2} `json:\"${1}\"`"
],
"description": "Add suffix for json Marshaller"
}
On the second usage of ${1} I want to replace the upper camel case by lower camel case. I guess I should use regex to make a substitution but as soon as I'm trying to do anything with regex my brain just runs away.
Could you help me with that?
I know I should show you what I attempted but believe me, it's irrelevant.
You want to turn the first character of the input word to lower case. So, you may use a simple ^(.) regex to find that first char and capture it into Group 1 and then use ${1:/downcase} to replace with a lowercase version of that character:
"body": [
"${1} ${2} `json:\"${1/^(.)/${1:/downcase}/}\"`"
],
This is a "rough" demo of how it works.
I'm using the Kibana console to perform such queries (they are separated: one for the hashtags, one for the mentions). The collection of documents are blog entries with a textContent field, which may have user mentions like #theUserName #AnotherOne or hashtags like #helloWorld and #hello2. The queries look like the following one:
GET /xblog/_search
{
"source": [
"id",
"textContent"
],
"query": {
"regexp": {
"textContent": {
"value": "#([^-A-Za-z0-9])",
"flags": "ALL"
}
}
}
}
But the problem is it's returning also the documents that do not contain a #userMention. I think the # in the regex is being treated as a special symbol, but reading the documentation I couldn't find how to escape it.
Inthe docs 1, the authors say that you can escape any symbol with double quotes, so I tested:
""#""
But I got nothing.
I also testes expressions I'm used to, like:
/\s([##][\w_-]+)/g
But that produces multiple errors in Kibana. I tried replacing some parts according to the documentation, but it's still not working.
Can you point me in the right direction?
Thanks in advance,
You enabled the ALL flag that makes # match the whole string, see the ElasticSearch regex documentation:
If you enable optional features (see below) then these characters may also be reserved:
# # & < > ~
Then, in the Any string section:
The at sign "#" matches any string in its entirety.
Enabled with the ANYSTRING or ALL flags.
Since you do not need any special behavior here you may simply tell the engine to use a "simple" regex by passing "flags": "NONE", or escape the #, "\\#([^-A-Za-z0-9])":
Any reserved character can be escaped with a backslash "\*" including a literal backslash character: "\\"
And since you need a whole string match, you may need to add .* on both ends (to match strings containing your match):
"query": {
"regexp": {
"textContent": {
"value": ".*#[^-A-Za-z0-9].*",
"flags": "NONE"
}
}
}
Or
"query": {
"regexp": {
"textContent": {
"value": ".*\\#[^-A-Za-z0-9].*",
"flags": "ALL"
}
}
}