pcre expression for irc nicknames? - php

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

Related

Regex to validate username not working

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.

secured regular expression that restrict specific special characters

I tried to create regular expression with specification below
any alphabetic character (at least one)
any numeric character (at least one)
no spaces
accept all special characters (except ",;&|')
^(?=.*[0-9])(?=.*[a-z])(?!.*\s)((?!.*[",;&|'])|(?=(.*\W){1,}))(?!.*[",;&|'])$
This is the one I tried.
What I can do with this?
Question is still vague in nature, please provide some examples of accepted strings.
Just to get you started you can use:
character class in a negative lookahead
Don't forget start & end anchors:
Regex:
/^(?=.*?\d)(?=.*?[a-z])(?!.*?[ ",;&|']).+$/i
This regex will match 1 or more characters that are not one of ",;&|' and atleast one digit and a-z alpgabet is required.
Live Demo: http://www.rubular.com/r/nxdi79ZcRx
In PHP use it like this:
'/^(?=.*?\d)(?=.*?[a-z])(?!.*?[ ",;&|\']).+$/i'

Regular expression for validating a username?

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

Check a variable using regex

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

preg_match in php

I want to use preg_match() such that there should not be special characters such as ``##$%^&/ '` in a given string.
For example :
Coding : Outputs valid
: Outputs Invalid(String beginning with space)
Project management :Outputs valid (space between two words are valid)
'Design23' :Outputs valid
23Designing : outputs invalid
123 :Outputs invalid
I tried but could not reach to a valid answer.
Does a regex like this help?
^[a-zA-Z0-9]\w*$
It means:
^ = this pattern must start from the beginning of the string
[a-zA-Z0-9] = this char can be any letter (a-z and A-Z) or digit (0-9, also see \d)
\w = A word character. This includes letters, numbers and white-space (not new-lines by default)
* = Repeat thing 0 or more times
$ = this pattern must finish at the end of the string
To satisfy the condition I missed, try this
^[a-zA-Z0-9]*\w*[a-zA-Z]+\w*$
The extra stuff I added lets it have a digit for the first character, but it must always contain a letter because of the [a-zA-Z]+ since + means 1 or more.
Try
'/^[a-zA-Z][\w ]+$/'
If this is homework, you maybe should just learn regular expressions:
Regular expressions tutorial
Regular expressions reference
PCRE syntax reference for PHP

Categories