Wrapping all keys in double curly in YML file with Sublime - sublimetext2

I'd like to do a quick manipulation on a YML file that'll wrap all keys in double curly braces and strip the quotations , such as:
level:
group:
continue: "continue"
stop: "stop"
go: "go"
halt: "halt"
becomes
{{level:}}
{{group:}}
{{continue:}} continue
{{stop:}} stop
{{go:}} go
{{halt:}} halt
I'm sure there's a way to do this using multicursor, but so far no luck.
Thanks!

Use Regular Expression!
You want to do a replace (Find->Replace...), searching for (\w+?:), and replace with {{\1}}.
This should do what you expect (I'll add a little explanation when I'm back on my laptop).
\w is for any alphanumeric characters, and things like _, so that would be your identifier
+?: any number (but at least one) of said set of character, but in a non-greedy way (or it would also match the : I put after)
: is obviously the end of your identifier
(...) means you capture everything inside, and store it in \1
That's why the replace is: {{\1}}
Demo:
http://regex101.com/r/yE5pM1/1

Related

Godot/gdscript strings with escape characters from database

I'm making a dialogue system in gdscript and am struggling with escape characters, specifically '\n'.
I'm using CastleDB as, although not perfect, it has allowed me to have almost everything stored in data and will allow the person doing the writing for the game to do everything outside the engine, without me having to copy and paste stuff in.
I've hit a stumbling block with escape characters. A single text entry in CastleDB doesn't support spaces, and '\n' within the string prints to '\n', not a space, in the dialog box.
I've tried using the format string function with 'some text here {space} some more text', with the space referencing a string consisting of just \n. This still prints \n. If I feed some constant string with \n in the middle directly into the function which displays the dialog text, it adds a space so I'm not really sure what is going on here.
I don't have a computer science background (I've done some C up until pointers, at which point I decided to return later).
Is there something going on in the background with my string in gdscript? It prints out just like you would expect a string to, apart from ignoring my escape characters.
Could it be something to do with the fact that it comes in as a JSON? As far as I'm aware, even if a string is chopped up and reassembled, it should still just behave like a string...?!
Anyway, I haven't included any code because I don't know what code you'd need to see. I'm hoping it's something simple that because I'm teaching myself as I go I just wasn't aware of, but can post code if it helps.
Thanks,
James
Escape sequences are a way of getting around issues with syntax. When you type a string in most programming languages, it starts with " and ends with another ". And it needs to stay on one line. Simple, right?
What if you want to put an actual " in your string? Or a new line? We need some way of telling the compiler, "hey, we want to insert a newline here, even though we can't use an actual newline character". So we use a \ to let the compiler know that the next character is part of an escape sequence.
This causes another problem: What if we literally want to put a backslash in a string? That's where the double backslash comes from: \\ is the escape sequence for \, since \ by itself has a special meaning.
But CastleDB (apparently, I'm not familiar with it) doesn't recognize escape sequences, so when you type \n it thinks you literally want \ followed by n. When it converts this to JSON, it inserts the \\ because JSON does recognize escape sequences.
GDScript also recognizes escape sequences, so print("Hello\nworld!") prints
Hello
world!
You could try input_string.replace("\\n", "\n") to replace the \n escape sequences.
I've solved this by looking at the way CastleDB data is stored on the project's github page.
For some reason "\n" was stored as "\\n" behind the scenes. Now that I know why it was printing weirdly I can change it, even though it feels like a messy solution!
To add even more weirdness to this whole backslash business, stack overflow displays a double backslash as a single backslash so I have to write \ \ \n minus the spaces to get \\n...
I'm sure there must be a reason, but it eludes me.

How to use find & replace to wrap each line in document with quotations and a comma after in VS Code [duplicate]

regex difference between vscode and visual studio
starting with
line1
line2
find: ^(.+)$
replace: "$1",
In vscode it works as expected, resulting in
"line1",
"line2",
In studio, doesn't seem to work, resulting in
"line1
",
"line2
",
Which one is correct? I assume vscode.
TL;DR: Use ^(.*[^\r\n]) to match a whole line without EOL characters.
According to the Docs:
Purpose
Expression
Example
Match any single character (except a line break)
.
a.o matches "aro" in "around" and "abo" in "about" but not "acro" in "across"
Anchor the match string to the end of a line
\r?$
car\r?$ matches "car" only when it appears at the end of a line
Anchor the match string to the end of the file
$
car$ matches "car" only when it appears at the end of the file
However, some of that doesn't seem to hold true for some reason (i.e., . does match a line break and .$ does match the end of any line). All of the following patterns will match from the beginning to the end of the line including EOL characters: ^.+, ^.+$, ^.+\r?$.
I have noticed this behavior in VS2017 before and I'm not sure why it happens but I was able to get around it using something like the following:
^(.*[^\r\n])
Note: You can also get rid of the capturing group and replace with "$0",.
In VSCode regex patterns, a dot . matches any char but any line break chars.
In .NET regex used in Visual Studio, a dot matches any char but a newline, LF, char.
This difference explains the results you get and you can't call them right or wrong, these are just regex engine differences.
Note you would not have noticed any difference between the two engines if you had used LF-only line endings, but Visual Studio in Windows uses CRLF endings by default.
In order to wrap a whole line with double quotes using .NET regex, just exclude both LF and CR (carriage return) symbols from matching by replacing the dot with a [^\r\n] negated character class:
^[^\r\n]+
And replace with "$&", pattern where $& refers to the whole match.
You may get rid of the capturing group in the VSCode regex and use the same replacement pattern as in .NET, too.

Escape square bracket in Tcl_StringCaseMatch

I am using Tcl_StringCaseMatch function in C++ code for string pattern matching. Everything works fine until input pattern or string has [] bracket. For example, like:
str1 = pq[0]
pattern = pq[*]
Tcl_StringCaseMatch is not working i.e returning false for above inputs.
How to avoid [] in pattern matching?
The problem is [] are special characters in the pattern matching. You need to escape them using a backslash to have them treated like plain characters
pattern= "pq\\[*\\]"
I don't think this should affect the string as well. The reason for double slashing is you want to pass the backslash itself to the TCL engine.
For the casual reader:
[] have a special meaning in TCL in general, beyond the pattern matching role they take here - "run command" (like `` or $() in shells), but [number] will have no effect, and the brackets are treated normally - thus the string str1 does not need escaping here.
For extra confusion:
TCL will interpret ] with no preceding [ as a normal character by default. I feel that's getting too confusing, and would rather that TCL complains on unbalanced brackets. As OP mentions though, this allows you to forgo the final two backslashes and use "pq\\[*]". I dislike this, and rather make it obvious both are treated normally and not the usual TCL way, but to each her/is own.

CTRL+F search for "xx *anything* xx"

I am trying to find and replace all for loops in my (js) code with slightly different syntax. I want to find every for loop that used the syntax "for ( any code here ){". Is there a way to find all such instances?
That's a regular expression question I think. In SublimeText2 start the search functionality. Make sure regular expressions are on (first button, labeled .*) and the search for for\s*\(.*?\)\s*\{.
Enable the regex option and type for \(.+\)\{
Explanation:
the backslashes "escape" the parentheses and brace. In other words, they tell the regex that those are the characters within the search and not part of a regex command. The . searches for any character and the + modifies that to include one or more instances of any character.
Here's a screen shot of sublime text
You want to search by regular expression. Notepad++ supports this, not sure about Sublime Text but I would image it does also. With regular expression enabled, search for
xx.+xx
This will search for the characters xx, followed by any character (.) as many times as it can find it (+), followed by the characters xx. This should give you the result you are looking for.
Here is a article with some information about using regular expressions in Notepad++

How to convert a glob with braces to a regex

I want to change a glob such as c{at,lif} in to a regex. What would it look like? I've tried using /c[at,lif]/ but that did not work.
For Basic GREP operation see f.e. http://www.regular-expressions.info/refquick.html
From http://www.regular-expressions.info/alternation.html:
If you want to search for the literal text cat or dog, separate both options with a vertical bar or pipe symbol: cat|dog. If you want more options, simply expand the list: cat|dog|mouse|fish.
This suggests the following should work:
/c(at|lif)/
Obligatory What Was Wrong With Yours, Then:
/c[at,lif]/
The square brackets [..] are not used in GREP for grouping, but to define a character class. That is, here you create a custom class which allows one of the characters at,lif. Thus it matches ca or c, or cf -- but always only one character. Adding a repetition code c[at,lif]+ only appears to work because it will then match both cat and clif, but also cilt, calf, and c,a,t.