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.
Related
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.
I would like to modify the value of a single key in a larger json response body using Charles proxy re-write.
As an example, I want to change age from 20 to 30 (but can be any initial value):
{
"userId": "some_value_i_dont_want_to_touch",
"username": "Charlie",
"age": "20"
}
I do not wish to replace the entire json body as that is not practical for my situation; I just want to modify the value of a single key.
The regex "age":[\s\S]"(.*)" or "age":[\s\S]"[^"]+" highlights the text I want on sites like https://regex101.com/
I've tried a few variants of this as well, but none of them seem to actually change the value.
Example screenshot
If I just enter the Match value "20" and the Replace value as "30" it works which tells me I have the url and path correct, but I'd like to avoid accidentally replacing the wrong thing.
Using the current full value without regex doesn't work either, so trying to do "age": "20" -> "age": "30" doesn't work, but maybe I need to escape something, though this would also limit me to cases where I know the value beforehand which is not ideal.
The error logs also indicate the request was re-written, but nothing changes
Ex: Running: Body: "age":[\s\S]"(.*)" -> "age": "30"
I am not clear on how the groups work (with the $) but maybe this has something to do with it.
I am probably just missing something obvious or going about this wrong.
Any insight would be appreciated.
For this topic, perhaps there is no whitespace needed because a proxy response is a text string with no spaces, however when we view it in JSON Text it helps us to read(thus having white spaces)
For the rest like me who is new that may help!
I just learnt that for [ needs to be escaped for regex to work, etc:
Match:
"xxxx":\[] #tick regex
or you may,
^"xxxx.{0,}"
xxxx refering to anything that starts with (^)
" at the end marks the end of the string or else it'll match everything else
Replace:
"xxxx":\["new"]
Hope this helps!
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
I have JSON objects in this format:
{
"1f626": {
"name": "frowning face with open mouth",
"ascii": [],
"code_points": {
"base": "1f626",
"default_matches": [
"1f626"
],
"greedy_matches": [
"1f626"
],
"decimal": ""
}
}
}
I have to remove the code_points object using Regular Expressions.
I have tried using this RegEx:
(("code\w+)(.*)(}))
But it is only selecting the first line.
I have to select until end of curly brackets in order to fully get rid of the code_points object.
How can I do that?
Note: I have to remove it using Regular Expressions and not JavaScript.
Please don't post any JavaScript answers or mark this as a possible duplicate of a JavaScript-based question.
Alternatively, at the command-line, if you can use jq
jq "del(.[].code_points)" <monster.json >smaller_monster.json
This deletes the code_points key inside each 2nd-level object.
It took my machine about 5 seconds on a 60MB document.
It's not a regular expression but it's not JavaScript, either. So, it meets half of your non-functional requirements.
("code_points")([\s\S]*?)(})
The problem you had is that . is actually any character except \n, so in this case I usually use [\s\S] which means any whitespace and non-whitespace character (so it's actually any character). Also you should make * quantifier to be lazy by adding ?.
Remember that this Regular Expression won't work properly in case you have inner object (other {}) in code_points
I'm using Logstash to get some text out of a string and create a field.
The string of the message is:
"\"07/12/2016 16:21:24.652\",\"13.99\",\"1467351040\""
I can't figure it out how to get three results, being the first:
07/12/2016 16:21:24.652
The second
13.99
The third
1467351040
match => {
"message"=> [
"\\"%{DATESTAMP:a}\\",\\"%{NUMBER:b}\\",\\"%{NUMBER:c}\\""
]
}
To help the next time you have to craft a grok pattern:
GrokConstructor, to test your pattern
The main patterns
Grok filter documentation
That's the correct line indeed.
I had to remove one backslash for my own config. Thanks very much. Saves me a lot of time and stuff.
grok{ match => { "message"=> [ "\"%{DATESTAMP:a}\",\"%{NUMBER:b}\",\"%{NUMBER:c}\"" ]} }