$.validator.addMethod('AZ09_', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, 'Only letters, numbers, and _-. are allowed');
When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --
Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:
/^[a-zA-Z0-9._-]+$/
Escaping the hyphen using \- is the correct way.
I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.
(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)
The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.
In a server side string: \\-
On the client side: \-
In regex (covers): -
Or you can simply put at the and of the [] brackets.
Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/
In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.
\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3
A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression
Related
$.validator.addMethod('AZ09_', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, 'Only letters, numbers, and _-. are allowed');
When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --
Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:
/^[a-zA-Z0-9._-]+$/
Escaping the hyphen using \- is the correct way.
I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.
(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)
The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.
In a server side string: \\-
On the client side: \-
In regex (covers): -
Or you can simply put at the and of the [] brackets.
Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/
In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.
\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3
A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression
$.validator.addMethod('AZ09_', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, 'Only letters, numbers, and _-. are allowed');
When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --
Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:
/^[a-zA-Z0-9._-]+$/
Escaping the hyphen using \- is the correct way.
I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.
(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)
The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.
In a server side string: \\-
On the client side: \-
In regex (covers): -
Or you can simply put at the and of the [] brackets.
Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/
In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.
\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3
A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression
I need to make a URL pattern which could work with this URL:
mysite.com/blog/12/بلاگ-مثال
It contains utf-8 characters so I tried using \X:
re_path(r'^blog/?P<blog_id>[\d+]+/(?P<slug>[\X.*]+)/$', views.single_blog, name='single_blog')
But it didn't work. I don't know why. Maybe just because I'm not good in regex. So I tried a different pattern using just .* to accept anything:
re_path(r'^blog/?P<blog_id>[\d+]+/(?P<slug>[.*]+)/$', views.single_blog, name='single_blog')
But this also doesn't work and I get:
The current path, blog/12/بلاگ-مثال, didn't match any of these.
So as I mentioned I'm not good in regex, what's the right way to fix this?
Is it the right time to say now I have two problems or regex is the only way?
Your approach to match something did not work since \X is not supported by Python re and [.*]+ matches 1+ dots or asterisks, but not any chars (because you put .* into [...] character class where they denote literal symbols, not special chars).
Besides, [\d+]+ is also a character class matching any digit or +, 1 or more times, so there is also a problem.
You may use a [^/] negated character class to match any char but /:
r'^blog/(?P<blog_id>\d+)/(?P<slug>[^/]+)/?$'
Details
^ - start of input
blog/ - a literal substrig
(?P<blog_id>\d+) - Group "blog_id": 1+ digits
/ - a /
(?P<slug>[^/]+) - Group "slug": 1+ chars other than /
/? - an optional /
$ - end of string.
Here is a regex demo (note highlighting characters from the Arabic script is not working there.)
Is it the right time to say now I have two problems ...
In fact, you have chosen the right job for this task.
The other answer seems valid but can't tolerate to have the word Persian in it. I'm posting this answer to throw some points of why your own regex doesn't work as expected.
?P<blog_id>[\d+]+
Probably you meant a named group here, the same as the one you used later in regex. You missed opening and closing parentheses: (?P<blog_id>[\d+]+). Also [\d+] means a character class consisted of digits and +. You need to remove +: (?P<blog_id>[0-9]+)
(?P<slug>[\X.*]+)
Construction is fine as it should be but character class is not. \X doesn't have a special meaning in a character class, let alone Python that doesn't support it by its re module even. .* is no exception. In a character class almost all special tokens are treated literally.
So [\X.*] matches a X or a . or an asterisk *. You need to change it to something more general like [^/]+ which means match up to the first slash (= match anything except forward slash).
I have a csv file full of values such as this:
0.00145423,3.03795e-05
I wanted to check that all the lines were consistent so I tried to grep for any unexpected characters like so...
grep '[^0-9,e\-\.]' myfile
In my mind it goes like this: find a line with any character [] that is not ^ a number 0-9, comma ,, letter e e, hyphen \- (attempted to escape with \), or a period \.. However, hyphens still continue match.
[EDIT]This does not happen in python, only with bash/grep:
>>> re.search("[^0-9,e\-\.]", "0.00145423,3.03795e-05")
>>>
unsatisfying solution:
If I move the escaped hyphen to the end it works:
grep '[^0-9,e\.\-]' myfile
Putting the escaped hyphen next to the 0-9 range results in grep: Invalid range end.
Can someone explain what's going on? Is this some bash argument parsing issue or something specific to grep?
bash4.3.33, grep2.21
The way to include a literal - in a character list is to put it in the first or last position of the bracket expression, exactly as shown in the answer at: Get final special character with a regular expression.
From POSIX 9.3.5 RE Bracket Expression:
The character shall be treated as itself if it occurs first (after an initial ^, if any) or last in the list, or as an ending range point in a range expression.
Some tools might have additional ways of doing it with some kind of escaping but you're always safe to just put it first or last.
Note that - isn't the only character that has different behavior depending where it shows up in a bracket expression. Consider ], and ^ as well.
Remember that - is a range operator, so \-\ matches any character in the range \ to \, which is exactly a \.
If you move it to the end, it'll loose its meaning as a range, that's why it works.
I'm working on a regular expressions pattern, but it contains a number of special characters. I'm not really sure how to incorporate them in a normal regex pattern string. Specifically, I need to test to see if a string contains '+/-'...
I've tried using quotes etc but have no luck (I'm extremely new to regex). I am coding this in C# 4.0.
One string example is "3Z1Z +/- 5.5"
Any help is much appreciated - Thanks a lot!
Create a simple regex :
foundMatch = Regex.IsMatch(SubjectString, #"\+/-");
Will return true if this sequence of characters is found anywhere in your string. The explanation is left as an exercise to you.
Read more here.
These are part of the special character list (see also). Basically, add them to the pattern by prefixing them with a backslash (\). e.g. + becomes \+
^\+|\-$ # + or -
The same would go for anything else with special meaning, such as ., {, }, (, ), ^, $, |, [, ], etc.
There are some exceptions though. For instance, when creating a class such as: [a-z] the hyphen (-) would have special meaning (all letters from a through z). So if you wanted a literal hyphen you'd have to escape it (unless it falls as the last character of the class). e.g.
[a-z-A-Z] # hyphen should be escaped if you wanted a literal hyphen
[a-z\-A-Z] # the "correct" counter-part
[a-zA-Z-] # actually legal because it's inserted as the last character
# and therefor treated as a literal hyphen despite not being
# escaped.