I have written following regular expression /^[A-Za-z0-9-_\s]*$/ in PHP which allows numbers, letters, spaces, hyphen and underscore. I want to display those matches which are not valid against the regex i.e "My Name is Blahblah!!!" should give me "!!!" output.
Use the caret symbol inside the character class to invert the match and remove the start (^) and end ($) characters:
/[^A-Za-z0-9-_\s]+/
http://php-regex.blogspot.com/2008/01/how-to-negate-character-class.html
If you replace all the matches with the empty string then you'll get the non-matching parts back:
preg_replace('/[A-Za-z0-9-_\s]+/', '', $string)
This will work for any arbitrary regex, but for your specific regex #Andy's solution is simpler.
Notice that I removed the anchors ^ and $ to make this work.
preg_replace("/^[A-Za-z0-9-_\s]*$/","","My Name is Blahblah!!!") // Output: "!!!"
Or, if you want all the groupings of them
preg_split("/^[A-Za-z0-9-_\s]*$/","","My Name is Blahblah!!!")
You have to put the hiphen - at the begining or at the end of the character class or escape it, so your regex would be :
/[^-A-Za-z0-9_\s]+/
or
/[^A-Za-z0-9_\s-]+/
or
/[^A-Za-z0-9\-_\s]+/
Related
Can you help me out with this one? I have a list of words like this:
sachbearbeiter/-in
referent/-in
anlagenführer/-in
it-projektleiter/-in
I want to select only:
sachbearbeiter/-in
referent/-in
This is my current regex: ([a-z]+)/-(in)
The problem is it hits all even the ones with - and with ü
Thank you in advance.
You can use anchors to match the word you want:
^([a-z]+)/-(in)$
^---- Here ----^
Working demo
Update: for your comment, if you want to accept aumlats you can use unicode flag with \w like this:
^(\w+)/-(in)$
Working demo
You need to specify beginning & end of string so that it can match exact chars
change your regex to
^([a-z]+)/-(in)$
^ -> stands for beginning of string
$-> for end of string
Your current regex i.e. ([a-z]+)/-(in) does escape the / character and also trying to look into substrings that matches the pattern, so it'll show each of them.
Regex should be : ^([a-z]+)\/-(in) i.e. it should start with only small case alphabets with escaped /
I want users input their username with only alphanumeric and dot character.
So I wrote a regex pattern as following:
'/([a-zA-Z0-9\.]+)/'
But I want to know is it the same with:
'/([a-zA-Z0-9.]+)/'
2 below patterns is the same? Thank you for help! :-)
You don't need to escape the dot which was present inside a character class. Inside a character class, dot . and escaped dot \. matches the literal dot. So both regexes are same.
And also for validation purposes, i would suggest you to add anchors like '/^[a-zA-Z0-9.]+$/' . Anchors would be used to do a exact string match. That is , /[a-zA-Z0-9.]+/ regex would match the substring foo in this ()foo input string but if you add start and end anchors to your regex like /^[a-zA-Z0-9.]+$/, it won't match even a single character in the above mentioned string. It's allowed to match only one or more alphanumeric or dot characters , if it finds a character other than dot or alphanumeric, then the regex engine won't match the corresponding string.
i am trying to find php regular expression that match the word like "Hello World" with space and also match the word "HelloWorld" without space.
You could use:
/^Hello ?World$/
Or you don't care the number of spaces:
/^Hello *World$/
Or it could also be blank chars like tab, then use \s instead a space.
Generally that would be:
/[a-zA-Z ]+/
If you want numbers, too:
/[a-zA-Z0-9 ]+/
You would need to set some sort of boundary. If the string just contains this, you can use start and end delimiters:
/^[a-zA-Z0-9 ]+$/
in my program php, I want the user doesn't enter any caracters except the alphabets
like that : "dgdh", "sgfdgdfg" but he doesn't enter the numbers or anything else like "7657" or "gfd(-" or "fd54"
I tested this function but it doesn't cover all cases :
preg_match("#[^0-9]#",$_POST['chaine'])
how can I achieve that, thank you in advance
The simplest can be
preg_match('/^[a-z]+$/i', $_POST['chaine'])
the i modifier is for case-insensitive. The + is so that at least one alphabet is entered. You can change it to * if you want to allow empty string. The anchor ^ and $ enforce that the whole string is nothing but the alphabets. (they represent the beginning of the string and the end of the string, respectively).
If you want to allow whitespace, you can use:
Whitespace only at the beginning or end of string:
preg_match('/^\s*[a-z]+\s*$/i', $_POST['chaine'])
Any where:
preg_match('/^[a-z][\sa-z]*$/i', $_POST['chaine']) // at least one alphabet
Only the space character is allowed but not other whitespace:
preg_match('/^[a-z][ a-z]*$/i', $_POST['chaine'])
Two things. Firstly, you match non-digit characters. That is obviously not the same as letter characters. So you could simply use [a-zA-Z] or [a-z] and the case-insensitive modifier instead.
Secondly you only try to find one of those characters. You don't assert that the whole string is composed of these. So use this instead:
preg_match("#^[a-z]*$#i",$_POST['chaine'])
Only match letters (no whitespace):
preg_match("#^[a-zA-Z]+$#",$_POST['chaine'])
Explanation:
^ # matches the start of the line
[a-zA-Z] # matches any letter (upper or lowercase)
+ # means the previous pattern must match at least once
$ # matches the end of the line
With whitespace:
preg_match("#^[a-zA-Z ]+$#",$_POST['chaine'])
I am trying to use preg_match to extract numbers only. I have tried the fllowing code but it does not work. Any ideas how to only get the number?
preg_match_all('/^[0-9]+$/i', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
Here's the regular expression you should be using:
/<userinfo>.*?(\d+).*?<\/userinfo>/
This will match
<userinfo>Jsome text here 16586 more text here.</userinfo>
and the 2nd element of the returned array (array[1]) will have the number you need. (16586)
Your regex shouldn't include the ^ and $ symbols, as they denote the start and end of the string, which doesn't exist in the middle of a string.
Take them out and the regex should work just fine.
The symbol ^ specify the beggening of the line and $ - line end. So your RegExp completely wrong. You should write
preg_match_all('/\d+/', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);
Also you do not need to specify i modificator, that means searching with case ignore mode.
You have used ^ and $ in your regular expression. ^ means match the start of the string and $ means match the end of the string. As such, you've written a regex to match a string that contains only a number; i.e. your regex says start of string then number then end of string. Remove these characters and you'll be able to match a number.
Have you tried?
preg_match_all('[0-9]+', '<userinfo>Jsome text here 16586 more text here.</userinfo>', $result);