I have this regex:
/^[A-z0-9\._-]{3,24}+$/i
This is supposed to only match a string between 3 and 24 characters long, and only with the characters a-z, A-Z, 0-9, and then also the . _ and - characters.
The problem is this is also matching strings like "^_^". Someone just created a username with exactly that, so this is definitely a problem! Can anyone spot the problem with my regex?
Use this regex:
/^[\w.-]{3,24}$/
A-z is not same as [A-Za-z] as it also includes other characters such as ^ (thanks Jack)
Also remove extra quantifier + after {3,24} which means one or more instances of whole string.
PS: I have also shortened your regex to use \w instead of [A-Za-z0-9_]
You do not need the + as you are specifying a range of lengths with the {3,24}
/^[A-z0-9\._-]{3,24}$/i
As was pointed out in the comments below, A-z matches the ^ character, as well. In this case A-Za-z would be better; however, the answer above with \w.- is the most elegant by far.
Related
I'm trying to do a regex that would accept the following:
/profile
/profile/photo
/profile/video
but would not accept anything else than /photo and /video:
I tried the following but without any luck:
/profile((\/(photo|video))|[^\w])/
Can someone tell me what's wrong with my regex.
Thanks
this'll work:
^\/profile(?:\/(photo|video))?$
about your regex: /profile((\/(photo|video))|[^\w])/
\w matches a-z, A-Z, 0-9, _ so ^\w will match any other character. for eg. profile/, profile%, profile# will result in a match. since you've written [^\w] and not [^\w]+ or * therefore it'll match a single non-word character after profile*here*
you've not prepended profile with a /
derived from what you've written, this'll work too:
^\/profile(\/(photo|video))?$
Try this:
/^\/profile(\/photo|\/video|)?$/
You can use this regex:
^\/profile(\/photo|\/video)?$
Here you have a working example:
http://regex101.com/r/iE2yJ4/1
I would like to use php's preg_match to capture substrings which comprise:
A-Z, a-z, all accented chars
space
hyphen
It must not capture strings with anything else in them, including numeric chars.
This example is close but also catches strings containing numeric chars:
preg_match("/([\p{L} -]+)/u", $string)
A similar question already had an answer (the one above) but it doesn't work...
If I understand your problem correctly (which I might not have), then you simply want to use the ^ and $ characters to specify that "the match HAS to start here and the match HAS to end here":
/^([\p{L} -]+)$/u
^ ^
Then preg_match would only return true if the string had nothing else in it.
DEMO
Edit:
If hyphens/spaces are only allowed in the middle:
/^([\p{L}](?:[\p{L} -]+[\p{L}])?)$/u
DEMO
Hey guys, I'm having a few issues with using PCRE to make a irc nickname format correctly. I'm not good with PCRE, and I'd love some suggestions from those of you who do use PCRE / regex. :)
I'm currently using this expression: /^([^A-Za-z]{1})([^A-Za-z0-9-.]{0,32})$/
I'm using it as such: preg_replace($regex, $replaceWith, $content)
I assumed this meant, starting from the front to the end, any characters that are not A-Z, a-z, or 0-9 for the first character, replace it. Any characters after that, in which are not A-Z a-z, 0-9, -, or ., replace it.
If anyone could help, you would be helping out greatly. It's the only thing stopping me from releasing a chat product to a new forum software. :/
I've been using the following regex to check for nicknames in my IRC logs:
/<([a-zA-Z\[\]\\`_\^\{\|\}][a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,31})>/
using it in a preg_match like so:
preg_match('/<([a-zA-Z\[\]\\`_\^\{\|\}][a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,31})>/', $line)
I simply check whether a user said something on the line and the line wasn't just a join/part message or nick change or something of the sort, but it would be easy to put it into a preg_replace too.
It matches the nicks according to the nickname rules in RFC 2812 Section 2.3.1, which state that the first character must be a letter (a-zA-Z) or special ([]{}^`|_\) and the rest of the characters may be letters, special, digits (0-9) or hyphens (-). I chose the max length of 32 based on GTAnet's NICKLEN=32 instead of the RFC's max length of 9, because many networks don't seem to follow this standard. The max length varies between different IRC networks so tweak it as required.
I'm not sure what you're trying to replace with, but it'd be better to check if the string matches a username (instead of not matching) and then replace if it doesn't:
$regex = '/^[a-z][a-z0-9.-]{0,32}$/i';
if (!preg_match($regex, $content))
{
// do your replace here
}
The regular expression says:
^ # Beginning of string
[a-z] # Match a single a-z
[a-z0-9.-]{0,32} # Match between 0 and 32 occurances of a-z, 0-9, . or -
$ # End of string
/i # Make the pattern case-insensitive
I'm still kinda new to using Regular Expressions, so here's my plight. I have some rules for acceptable usernames and I'm trying to make an expression for them.
Here they are:
1-15 Characters
a-z, A-Z, 0-9, and spaces are acceptable
Must begin with a-z or A-Z
Cannot end in a space
Cannot contain two spaces in a row
This is as far as I've gotten with it.
/^[a-zA-Z]{1}([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$/
It works, for the most part, but doesn't match a single character such as "a".
Can anyone help me out here? I'm using PCRE in PHP if that makes any difference.
Try this:
/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/
The look-ahead assertion (?=.{1,15}$) checks the length and the rest checks the structure:
[a-zA-Z] ensures that the first character is an alphabetic character;
[a-zA-Z0-9]* allows any number of following alphanumeric characters;
(?: [a-zA-Z0-9]+)* allows any number of sequences of a single space (not \s that allows any whitespace character) that must be followed by at least one alphanumeric character (see PCRE subpatterns for the syntax of (?:…)).
You could also remove the look-ahead assertion and check the length with strlen.
make everything after your first character optional
^[a-zA-Z]?([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$
The main problem of your regexp is that it needs at least two characters two have a match :
one for the [a-zA-Z]{1} part
one for the [^\s] part
Beside this problem, I see some parts of your regexp that could be improved :
The [^\s] class will match any character, except spaces : a dot or semi-colon will be accepted, try to use the [a-zA-Z0-9] class here to ensure the character is a correct one.
You can delete the {1} part at the beginning, as the regexp will match exactly one character by default
Im about to create a registration form for my website. I need to check the variable, and accept it only if contains letter, number, _ or -.
How can do it with regex? I used to work with them with preg_replace(), but i think this is not the case. Also, i know that the "ereg" function is dead. Any solutions?
this regex is pretty common these days.
if(preg_match('/^[a-z0-9\-\_]+$/i',$username))
{
// Ok
}
Use preg_match:
preg_match('/^[\w-]+$/D', $str)
Here \w describes letters, digits and the _, so [\w-]+ matches one or more letters, digits, _, and -. ^ and $ are so called anchors that denote the begin and end of the string respectively. The D modifier avoids that $ really matches the end of the string and is not followed by a line break.
Note that the letter and digits that are matched by \w depend on the current locale and might match other letter or digits than just [a-zA-Z0-9]. So if you just want these, use them explicitly. And if you want to allow more than these, you could also try character classes that are describes by Unicode character properties like \p{L} for all Unicode letters.
Try preg_match(). http://php.net/manual/en/function.preg-match.php