Django CharField storing newline - django

I have a CharField storing user input, including things like "\n" (literally, not by hitting "enter"), which is later used for parsing text with regular expressions. When I fetch the model from the database, however, comparing the value to '\n' fails - it's equal to '\n' instead. How do I unescape it? I do escape it later before insertion into the regular expression, since the field may contain e.g. "*", which I want to be interpreted literally. I tried mark_safe but no luck.

OK I figured it out, thanks to the answers in this question: Python Replace \\ with \
Basically, I needed to do something like that:
separator = processor.separator.decode('string_escape') # Decode
expr = r"<something>" + re.escape(separator) # Escape for the regular expression

Related

What is the regular expression for all pages except "/"?

I am using NextAuth for Next.js for session management. In addition, I am using the middleware.js to protect my routes from unauthenticated users.
According to https://nextjs.org/docs/advanced-features/middleware#matcher,
if we want to exclude a path, we do something like
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - static (static files)
* - favicon.ico (favicon file)
*/
'/((?!api|static|favicon.ico).*)',
],
}
In this example, we exclude /api, /static,/favicon.icon. However, I want to exclude all path except the home page, "/". What is the regular expression for that? I am tried '/(*)'. It doesn't seem to work.
The regular expression which matches everything but a specific one-character string / is constructed as follows:
we need to match the empty string: empty regex.
we need to match all strings two characters long or longer: ..+
we need to match one-character strings which are not that character: [^/].
Combining these three together with the | branching operator: "|..+|[^/]".
If we are using a regular expression tool that performs substring searching rather than a full match, we need to use its anchoring features; perhaps it supports the ^ and $ notation for that: "^(|..+|[^/])$".
I'm guessing that you might not want to match empty strings; in which case, revise your requirement and drop that branch from the expression.
Suppose we wanted to match all strings, except for a specific fixed word like abc. Without negation support in the regex language, we can use a generalization of the above trick.
Match the empty string, like before, if desired.
Match all one-character strings: .
Match all two-character strings: ..
Match all strings longer than three characters: ....+
Those simple cases taken care of, we focus on matching just those three-symbol strings that are not abc. How can we do that?
Match all three-character strings that don't start with a: [^a]...
Match all three-character strings that don't have a b in the middle: .[^b].
Match all three-character strings that don't end in c: ..[^c].
Combine it all together: "|.|..|....+|[^a]..|.[^b].|..[^c]".
For longer words, we might want to take advantage of the {m,n} notation, if available, to express "match from zero to nine characters" and "match eleven or more characters".
I will need to exclude the signin page and register page as well. Because, it will cause an infinite loop and an error, if you don't exclude signin page. For register page, you won't be able to register if you are redirected to the signin page.
So the "/", "/auth/signin", and "/auth/register" will be excluded. Here is what I needed:
export const config = {
matcher: [
'/((?!auth).*)(.+)'
]
}

Regex validator does not work with Django admin forms

I try to use a RegexValidator with a CharField, but I can't make it work...
class Configuration(models.Model):
name = models.CharField(verbose_name=u'Name', validators =
[RegexValidator(regex="[a-z]", message="Not cool", code="nomatch")])
I then just register it with
admin.site.register(Configuration)
But then in the admin forms, it accepts any possible name... Is the validation system suppose to work like that, or am I missing something ?
Your current regex checks that your value contains a single character from a-z. So it allows a, but it also allows a1.
Try changing the regex to:
regex=r"^[a-z]+$"
By including ^ and $ to mark the beginning and end of string, you make sure that your string contains only characters from a-z. The + allows multiple characters.

Setting regular expression to validate URL format in Adobe CQ5

I want to validate a URL inside a textfield using Adobe CQ5, so I set up the properties regex and regexText as usual, but for some reason is not working:
<facebook
jcr:primaryType="cq:Widget"
emptyText="http://www.facebook.com/account-name"
fieldDescription="Set the Facebook URL"
fieldLabel="Facebook"
name="./facebookUrl"
regex="/^(http://www.|https://www.|http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$/"
regexText="Invalid URL format"
xtype="textfield"/>
So when I type inside the component I can see an error message at the console:
Uncaught TypeError: this.regex.test is not a function
To be more accurate the error comes from this line:
if (this.regex && !this.regex.test(value)) {
I tried several regular expressions and none of them worked. I guess the problem is the regular expression itself, because in the other hand I have this other regex to evaluate email address, and it works perfectly fine:
/^[A-za-z0-9]+[\\._]*[A-za-z0-9]*#[A-za-z.-]+[\\.]+[A-Za-z]{2,4}$/
Any suggestions? Thanks in advance.
The syntax of your regex seems to treat the forward slashes (/) as special characters. Since you want to parse a URL containing slashes, my guess is you should escape them twice like this: '\\/' instead of '/'. The result would be:
/^(http:\\/\\/www.|https:\\/\\/www.|http:\\/\\/|https:\\/\\/)[a-z0-9]+([-.]{1}[a-z0-9]+)‌​*.[a-z]{2,5}(:[0-9]{1,5})?(\\/.*)?$/
You need to escape them twice because the string to be compiled as a regex must contain '\/' to escape the slashes, but to introduce a backslash in a string you have to escape the backslash itself too.

Wrap JSON field names in quotes using vim search and replace

I have a simple json file that isn't well formatted it looks like:
{ ID: '092558667',
NAME: 'Store Made',
PARENT_CATEGORY_ID: '692558669',
INCLUDED_IN_NET_SALES: '1' }
All I need to do is wrap the field names in double quotes. In vim the closest I have gotten is to wrap the field name and the colon in quotes - obviously I need to figure out how to get the string without the colon wrapped. Here's what I am trying:
:%s/[A-Z_]*:/"&"
If I leave the colon out of the query the whole file ends up being selected.
You can use capture groups:
%s/\([A-Z_]*\):/"\1":/
To handle already quoted keys properly:
%s/"\?\([A-Z_]*\)"\?:/"\1":/
Ok, with the information above I ended up with this:
:%s/[ \t]\([A-Za-z_].*\):/"\1":/
it supports upper- and lowercase chars
it skips already quoted fields
Since this can be considered a completion, I mapped it to a vim completion shortcut ctrl-x ctrl-j in .vimrc (they all start with ctrl-x ) :
:noremap <C-x><C-j> :%s/[ \t]\([A-Za-z_].*\):/"\1":/<CR>

How do I write this URL in Django?

(r'^/(?P<the_param>[a-zA-z0-9_-]+)/$','myproject.myapp.views.myview'),
How can I change this so that "the_param" accepts a URL(encoded) as a parameter?
So, I want to pass a URL to it.
mydomain.com/http%3A//google.com
Edit: Should I remove the regex, like this...and then it would work?
(r'^/(?P[*]?)/?$','myproject.myapp.views.myview'),
Add % and . to the character class:
[a-zA-Z0-9_%.-]
Note: You don't need to escape special characters inside character classes because special characters lose their meaning inside character sets. The - if not to be used to specify a range should be escaped with a back slash or put at the beginning (for python 2.4.x , 2.5.x, 2.6.x) or at the end of the character set(python 2.7.x) hence something like [a-zA-Z0-9_%-.] will throw an error.
You'll at least need something like:
(r'^the/uri/is/(?P<uri_encoded>[a-zA-Z0-9~%\._-])$', 'project.app.view'),
That expression should match everything described here.
Note that your example should be mydomain.com/http%3A%2F%2Fgoogle.com (slashes should be encoded).
I think you can do it with:
(r'^(?P<url>[\w\.~_-]+)$', 'project.app.view')