RegEx differences - php

Can someone please tell me the difference exactly between these 2 RegEx's?
'/[^a-zA-Z0-9\s]/'
and
'~[^A-Za-z0-9_]~'
Also, is there a syntax error for the space within the first Regex? Thinking it needs to be like this: /\s to be escaped properly.
Basically, I need a RegEx that only uses English A-Z, a-z, 0-9, and underscores only! Everything else will need to be replaced with an empty string ''. So, I know I need preg_replace to do this with, but Which RegEx is better to use, and why?
Thanks many guys!

The ^ inside your regex means NOT...and that is
[^a-zA-Z0-9]
means the string have not to have a-z, A-Z and 0-9 so if you want to replace all the chars which are not in those ranges (include the '_'), you have to use this statement:
$cleanString = preg_replace('/[^a-zA-Z0-9_]/', '', $theString);

The first character of the PCRE pattern string is a delimiter used to mark the end of the regular expression and the start of the modifier characters. The choice is arbitrary; you can use '/' or '~' or another character, but note that if you need the character in the expression part, then you will need to escape it.
In a character class, \s means any space character. Thus '/[^a-zA-Z0-9\\s]/' matches one character not in the set A-Z, a-z, 0-9, and space characters. '~[^A-Za-z0-9_]~' matches one character not in the set A-Z, a-z, 0-9, and underscore ('_').
One pattern string that meets your requirements is '~[^A-Za-z0-9_]+~s':
<?php
$str = <<<STR
test_
one
two Three 45
STR;
echo preg_replace('~[^A-Za-z0-9_]+~s', '', $str);
which outputs:
test_onetwoThree45
http://codepad.org/Ycl1WvR8

Related

not sure about my regelur expression

I always get stucked with the preg_match function.
I want the input to match with a-z, A-Z, 0-9, ##&-_., and nothing else.
So if ! is in the input, it need to return false.
What I have till now.
$string = "String-20";
return (preg_match("/[a-z][A-Z][0-9][##&-_.,]/i", $string)) ? true : false;
This should return true.
But keep return false.
You'll want to have those as one set rather than breaking them up like that. Your pattern would match a string like "aA0#"
You're saying "One character a-z, then one A-z, then one 0-9, then one of these special characters" but what you actually want is "Any number of these specific characters"
The ^ and $ mean start and end of the string so I think this should do what you want.
preg_match('/^[a-zA-Z0-9##&\-_.,]*$/i', $string)
Your regex matches 4 character chunk(s) anywhere inside a string (as preg_match can find partial matches): 2 letters, then a digit and some chars including uppercase letters because &-_ declares the following range:
Use
/^[a-z0-9##&_.,-]+$/i
or even (since \w here will match [a-zA-Z0-9_]):
/^[\w##&.,-]+$/
If an empty string is allowed, replace + (one or more occurrences) with * (zero or more occurrences).
The ^ anchor will make sure the engine starts matching at the beginning of the string and $ anchor will make sure the pattern should match up to the string end. The hyphen at the end of the character class will be parsed as a literal -.

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.

Validating Regular Sentence Grammar with Regex

So I want to validate a string based on whether or not it contains only grammatical characters as well as numbers and letters.(basically A-Z, a-z, 0-9, plus periods(.), commas(,) colons(:), semicolons(;), hyphens(-), single quotes('), double quotes(") and parentheses(). I am getting a PHP error that says "Compilation failed: range out of order in character clas". What regex code should I be using?
This is the one I'm currently using:
^[a-zA-Z0-9_:;-()'\" ]*^
You need to escape - which would then become this ^[a-zA-Z0-9_:;\-()'\" ]* . - has a special meaning inside character set so it needs to be escaped. ^ in the end is also not necessary. The regex can also simplified using \w like this
^[\w:;()'"\s-]*
\w matches letters, digits, and underscores.
The problem is that you have a dash character in the regex, which the parser is interpreting as a range instead of as a literal dash. You can fix that by:
Escaping it with a backslash (^[a-zA-Z0-9_:;\-()'\" ]*^)
Putting it at the start (^[-a-zA-Z0-9_:;()'\" ]*^)
Putting it at the end (^[a-zA-Z0-9_:;()'\" -]*^)

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

How do I check to see if my string contains spaces using preg_match?

i use this code in php to detect if the string contains characters other than a-z,A-Z,0-9. I want to check if the string contains any spaces. What should I add to the pattern?
if (preg_match ('/[^a-zA-Z0-9]/i', $getmail)) {
// The string contains characters other than a-z and A-Z and 0-9
}
If you want to add space to your current pattern, you actually need to input space in your pattern:
/[^a-z0-9 ]/i
^
Notice the space there
Useful Resources
You can try this
/[^a-z0-9\s]/i
As you used the i modifier no need for A-Z. The \s part will mean spaces, but also tabs or any white space.
if (preg_match('/[^a-z0-9 ]/i', $getmail)) // The string contains characters other than a-z (case insensitive), 0-9 and space
Note I removed A-Z from the character class too as you have case insensitivity turned on.

Categories