Search and Replace "(" " " and "[" in Vi - Regex - regex

I have this line:
[app_user] ([id], [nome], [email], [login], [senha], [tipo], [data])
I want to replace for this:
(id, name, email_address, username, password, access_type, created)
How I do that?
I try this (in vi):
:%s/\[app_user\] \+\\( \[id\], \+\[nome\], \+\[email\], \+\[login\], \+\[senha\], \+\[tipo\], \+\[data\]\\) /\\( id,name,email_address,username,password,access_type\\)/
But didn't work.
I think the problem is betwen " \+//(", because I try run, only simple search:
/\[app_user\] \+
And works.
I'm not expert in Regex.

To disable all special characters except the backslash in your search pattern use \V (very nomagic), that would make much easier to write it:
:%s/\V[app_user] ([id], [nome], [email], [login], [senha], [tipo], [data])/(id, name, email_address, username, password, access_type, created)

Related

Remove a String Located between Character

I have an email column in my table. How to update email column with removing string between underscore _ and #.
Example:
Input: andri_pasardigital#zcode.id
Output: andri#zcode.id
You can use this : _(.*)#
See Demo
Not a regex, but API: https://www.postgresql.org/docs/current/static/functions-string.html
Something along the lines of
create table T (id serial primary key, email text);
insert into T (email) values ('bal_hussa#palo.enf' )
with
select substring( email from 1 for position('_' in email)-1) ||
substring( email from position('#' in email))
from T
produces
bal#palo.enf
could also work to answer Remove a String Located between Character

Capturing preceding whitespace in Julia

I have a very long piece of code that I need to add to, and would prefer to do it using a script rather than write myself for fear of introducing errors.
I have commands that look like this
rename oldname newname
rename oldname2 newname2
I want to, whenever I see the command "rename" I want to add a "note" command
rename oldname newname
note newname: "A Note"
rename oldname2 newname2
note newname2: "A Note"
I am using Julia's read and write features to do this, and it has been very easy so far.
f = open("renaming.txt") # open input file
g = open("renaming_output.txt", "w") # open output file
for ln in eachline(f)
write(g, "$ln \n") # write the command, no matter what it is
stripped = lstrip("$ln") # Strip whitespace so I can use "startswith" command
if startswith(stripped, "ren")
words = split("$ln", " ") # split on space to get the "newvar" name
println("$ln \n") #check that I am working with a rename command
println("note ", words[3]":") # check that the note function prints
note_command = string("note ", words[3], ": \n") # construct the note command
write(g, note_command) #write it to the output file.
end
end
My issue is with the indentation. The above code writes the "note" command on the far left, without any indentation. However, Ideally I would like the note command to be indented one level further than the rename command. But I can't figure out how to capture all the preceeding whitespace.
I presume that the answer involves using the match and m.match functions, but I can't get it to work.
Any help would be appreciated.
On Julia 0.7 the simplest change in your code would be to replace
println("note ", words[3]":")
with
println(first(ls, search(ls, 'r')-1), " note ", words[3]":")
Using regular expressions you can write rx = r"(?:(?!r).)*" at the start of your code and then:
println(match(rx, ls).match, " note ", words[3]":")
In both cases we take care to retain the start of ls till 'r' in its original form.
With Julia 6.1, my solution, with the help the answer, is as follows
if startswith(stripped, "ren") & !startswith(stripped, "renvars")
leftpad = " " ^ search("$ln", 'r')
words = split(stripped, " ")
varname = string(leftpad, " note ", words[3], ": ", words[2], " \n")
print(varname)
write(g, varname)
end
With the leftpad = ^ search("$ln", 'r') being the key addition. Given that the left padding of my code is always tabs, I just insert the number of tabs as there are characters before the first r. This solution works in 0.6.1, but search may behave differently in .7.

How do I find all instances that have field: "username" inequivalent to its stripped "username"

In a pseudo-query, it would look something like this:
User.objects.filter(username != username.strip())
Example usernames that would match this search:
"hello "
" hello"
" hello "
Example usernames that would NOT match this search:
"hello"
"hello world"
Examples are not exhaustive (i.e. the whitespace can be in the form of tabs, newlines, etc).
Why are you storing usernames in your database with leading and trailing spaces? Trim them before you insert them instead of allowing dirty data in your database.
But to answer your question directly, you don't need to use extra to do a regular expression lookup, as the queryset API natively supports it.
This should do what you want:
User.objects.filter(username__regex=r"\s*" + user + r"\s*")
Things get a little messy on SQLLite, where regex lookups aren't natively supported, but it still works.
extra can be used. This is what it would look like for PostgreSQL:
User.objects.extra(where=["username != regexp_replace(username, '^\s+|\s+$', '', 'g')"])
from django.db.models import Q
User.objects.filter(Q(username__startswith=' ') | Q(username__endswith=' '))

Escape double quote Character in Kognitio

It seems that the Kognito console use this template for password reset (also create user)
ALTER USER user_name ALTER PASSWORD TO " "; -- we cannot include a " character in the password which would end up in syntax error.
How do we escape this and include a " character
Finally found one solution from the manual
ALTER USER user_name ALTER PASSWORD TO 'Pass"word' ESCAPE '"';

Parse string where "separator" can be part of data?

I have std strings like these:
UserName: Message
At first look it seems like an easy problem, but this issue is in that the name's last character could be a ':' and the first letter of the message part of the string could be a ':' too. The user could also have spaces in their name.
So A user might be names 'some name: '
and might type a message ' : Hello'
Which would look like:
'some name: : : Hello'
I do have the list (vector) of usernames though.
Given this, is there a way I could extract the username from this sort of string? (Ideally without having to iterate through the list of users)
Thanks
Try a regex like (\w+?):\ \w+.
If you can't gaurentee that the username won't contain a ":" characters, and you want to avoid iterating the entire list each time to check, you could try a shortcut.
Keep a vector of just the usernames that contain special chars (I'm imagining that this is a small subset of all usernames). Check those first, if you find a match, take the string after [username]: . Otherwise, you can simply do a naive split on the colon.
I would use string tokens
string text = "token, test string";
char_separator<char> sep(":");
tokenizer< char_separator<char> > tokens(text, sep);
BOOST_FOREACH(string t, tokens)
{
cout << t << "." << endl;
}
The way I would approach this is to simply find the first colon. Split the string there, and then trim the two remaining strings.
It's not entirely clear to me why there are additional colons and if they are part of the value. If they need to be removed, then you'll also need to strip them out.