My current regular expression should be correct, though I wouldn't expect so, it doesn't work properly. It won't return "Got Match"
My currrent code is as follows:
$id = "http://steamcommunity.com/id/TestID";
if (preg_match("^http://steamcommunity\.com/id/.*?\n$", $id)) {
print "Got match!\n";
}
You're missing delimiters on your regex:
if (preg_match("#^http://steamcommunity\.com/id/.*?\n$#", $id)) {
^--here ^--here
Note that I've used # as the delimiter here, since that saves you having to escape all of the internal / charrs, if you'd used the traditional / as the delimiter.
You need a delimiter, like this:
if (preg_match("#^http://steamcommunity\.com/id/.*?$#", $id)) {
^ ^
And what's with the newline at the end? Surely you don't need that.
You're missing delimiters. For example:
"#^http://steamcommunity\.com/id/.*?\n$#"
Also, you're trying to match a newline (\n) that isn't in your string.
You need to add the pattern delimiter:
$id = "http://steamcommunity.com/id/TestID";
if (preg_match("#^http://steamcommunity\.com/id/.*?(\n|$)#", $id)) {
print "Got match!\n";
}
There are a couple of things that are wrong with it. First of all, you need to delimit the start and end of your regex with a character. I used #. You're also matching for a new line at the end of your regex, which you don't have and likely won't ever have in your string.
<?php
$id = "http://steamcommunity.com/id/TestID";
if (preg_match("#^http://steamcommunity\.com/id/.*?$#", $id)) {
print "Got match!\n";
}
?>
http://codepad.viper-7.com/L7XctT
First of all, your regex shouldn't even compile because it's missing delimiters.
if (preg_match("~^http://steamcommunity\.com/id/.*?\n$~", $id)) {
^---- these guys here -----^
Second of all, why do you have a \n if your string doesn't contain a new line?
And finally, why are you using regex at all? Effectively, you are just trying to match a constant string. This should be equivalent to what you are trying to match:
if (strpos($id, 'http://steamcommunity.com/id/') === 0) {
You need to have starting and ending delimiter in your pattern like /pattern/ or #pattern# or with brackets (pattern). Why is that? To have some pattern modifiers after ending delimiter like #pattern#i (ignore case)
preg_match('(^http://steamcommunity\.com/id/.*?\n$)', $id)
As the say your patten is start and end wrong. (Delimiter)
But this will be a better match of a 64-bit Steam ID. (Minimum 17 and Maximum 25 numbers)
if( preg_match("#^http://steamcommunity\.com/id/([0-9]{17,25})#i", $id, $matches) )
{
echo "Got match! - ".$matches;
}
I believe that there is no need for you to require that the string must end with a line break.
Explanation.
http://steamcommunity\.com/id/([0-9]{17,25})
^--- string ---^^-- Regexp --^
[0-9] - Match a number between 0 to 9
{17,25} - Make 17 to 25 matches
() - Returns match
Or use pattern as those (It is the same):
/^http:\/\/steamcommunity\.com\/id\/([0-9]{17,25})/i
(^http://steamcommunity\.com/id/([0-9]{17,25}))i
Regular Expressions PHP Tutorial
Online regular expression testing <- Dont use delimiter.
<?php
# URL that generated this code:
# http://txt2re.com/index-php.php3?s=http://steamcommunity.com/id&-1
$txt='http://steamcommunity.com/id';
$re1='(http:\\/\\/steamcommunity\\.com\\/id)'; # HTTP URL 1
if ($c=preg_match_all ("/".$re1."/is", $txt, $matches))
{
$httpurl1=$matches[1][0];
print "($httpurl1) \n";
}
#-----
# Paste the code into a new php file. Then in Unix:
# $ php x.php
#-----
?>
Resorces:
http://txt2re.com/index.php3?s=http://steamcommunity.com/id&-1
Related
How to match any thing dosen't contain a specific word using RegExp
Ex:
Match any string doesn't contain 'aabbcc'
bbbaaaassdd // Match this
aabbaabbccaass // Reject this
If you're just after this sequence of characters, don't use a Regular Expression. Use strpos().
if (strpos('aabbaabbccaass', 'aabbcc') !== false) {
echo 'Reject this.'
}
Note: Be sure to read the warning in the manual about strpos() return values.
You can use negative lookahead:
(?!.*?aabbcc)^.*$
Live Demo: http://www.rubular.com/r/4Exbf7UdDv
PHP Code:
$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
echo "accepted\n";
else
echo "rejected\n";
try this to avoid some sequences of letters :
^((?!aabbcc).)*$
try this:
if(preg_match('/aabbcc/', $string) == 0) {
[ OK ]
}
else {
[ NOT OK ]
}
You can use this to describe a substring that doesn't contain aabbcc:
(?>[^a]++|a(?!abbcc))*
for the whole string, just add anchors (^ $)
This question already has answers here:
Warning: preg_replace(): Unknown modifier
(3 answers)
Closed 3 years ago.
Does anyone knows why i receive this error : preg_match() [function.preg-match]: Unknown modifier '('
using this method:
function checkFBDateFormat($date) {
if(preg_match ("/^([0-9]{2})/([0-9]{2})/([0-9]{4})$/", $date, $parts)){
if(checkdate($parts[2],$parts[1],$parts[3]))
return true;
else
return false;
} else {
return false;
}
}
You did not escape your "/" and you didn't complete your if statements either, please try this:
function checkFBDateFormat($date) {
if(preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $date, $parts)){
if(checkdate($parts[2],$parts[1],$parts[3])) {
return true;
} else {
return false;
}
} else {
return false;
}
}
echo var_dump(checkFBDateFormat('08/09/2012'));
If the first char is e.g. an slash / is detected as delimiter fro the regular expression. Thus your regex is only the part ^([0-9]{2}). And everything after the second slash is recognized as modifiers for the regex.
If you really want to match a slash, use \/ to escape it
Since you are using slash in regular expression, need use other delimiter, try:
preg_match ("#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#", $date, $parts)
You need to escape your slash, like so:
if(preg_match ("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $date, $parts)){
You use / as delimiter for your expression. However, it's completely unnecessary anyway
$parts = explode('/', $date);
Even better: http://php.net/datetime.createfromformat
To give you an idea what happens: PCRE regular expression require a delimiter at the start and the end of the pattern itself. Everything after the second delimiter is treated as modifier. Thus you decided to use / as delimiter (it's always the first character), so your pattern ended right after /^([0-9]{2})/. Everything next (which is a ( at first) is treated as modifier, but ( is not an existing modifier.
If you want to stay with regular expression, I recommend to use another delimiter like
~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~
#^([0-9]{2})/([0-9]{2})/([0-9]{4})$#
Just read the manual about the PCRE-extension
Two additional comments:
You should define $parts, before you use it
Remember, that the expression is quite inaccurate, because it allows dates like 33/44/5678, but denies 1/1/1970
You might want to consider not using regular expressions at all.
<?php
// simple example
$timestamp = strtotime('12/30/2012');
if ($timestamp) {
// valid dateā¦ Now do some magic
echo date('r', $timestamp);
}
I want a regex solution to allow only
http://www.imdb.com/title/ttANYNumberWOrdetc/ links
Otherwise SHOW us error.. Incorrect link
I am not too good with regex
I just create this petren ..
preg_match('/http:\/\/www.imdb.com\/title\/(.*)\//is', 'http://www.imdb.com/title/tt0087469/', $result);
Its show me corect result but i think i missed some thing..
Thanks,
How about something like this: http://(?:www\.)?imdb.com/title/tt[^/]+/.
Example:
<?php
if ( preg_match('#^http://(?:www\.)?imdb\.com/title/tt[^/]+/$#', 'http://www.imdb.com/title/tt0448303/') )
echo 'Matches' . PHP_EOL;
Explanation:
The regular expression matches a string that starts with http:// followed either by imdb.com or www.imdb.com, then /title/tt followed by any character except for a / and that ends with a /.
The # is the delimiter, the ^ indicated the beginning of the string and the $ the end.
This should work:
if (preg_match("#^(http://www.|https://www.)imdb.com/title/tt([a-zA-Z0-9]+)(?:/)(?:^[a-zA-Z0-9]+)?$#s", 'http://www.imdb.com/title/tt0364845/', $matches)) {
echo 'yay';
} else {
echo 'nay';
}
good evening,
i need to check if the input match my regex or not
i use this pattern '#^[a-zA-Z\#]{3,30}$#is'
<?php
if( preg_match('#^[a-zA-Z\#]{3,30}$#is', 'input#input') ){ echo 'matched'; }else{ echo 'no match'; }
?>
if i removed the # char the regex still return TRUE
<?php
if( preg_match('#^[a-zA-Z\#]{3,30}$#is', 'inputinput') ){ echo 'matched'; }else{ echo 'no match'; }
?>
i need to edit the regex so it should contain the # char
You can use a look-ahead assertion to assert that:
/^(?=[^#]*#)[a-zA-Z#]{3,30}$/is
Here the look-ahead assertion (?=[^#]*#) ensures that there is at least one #.
It's very simple - just use strpos() to find # character in string. For example
if(strpos('#','thestring')!==FALSE)
{
/* it contains # */
}
else
{
/* it doesn't */
}
try this:
%^[a-z]{2,29}#[a-z#]{2,29}$%is
Several Issues I also fixed:
You don't need to use [a-zA-Z] if you specified the i controller for case insensitivity.
You can change the delimiters to whatever (almost) character you'd like, if you know a character will be in the regex pattern, don't use it as delmiter.
try this
~^[a-z]?#[a-z#]+$~i
If you need only 1 # char between a-z blocks you may use
~^[a-z]+#[a-z]+$~i
if you want to check full length of string you may use strlen, if you want do it for every part separately just replace + by {mincnt,maxcnt}
i need to write a case which only except the a-zA-Z0-9 characters with underscore and white space(1 or more than 1) and ignore all rest of the characters.I wrote a code but its not working properly.
In those case should be wrong but its show OK
1) test msg#
2) test#msg
3) test!msg
also those should be OK but currently shows wrong.
1) test msg.-(Two white space)
what i should to change in my code .pls help and see my code below.
$message=$_GET['msg'];
if(preg_match('/[^A-Za-z0-9]\W/',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}
Here's an optimized version of the one left by riad:
$message = $_GET['msg'];
if ( preg_match('/^[a-z0-9_ ]+$/i', $message) )
{
echo 'Ok';
}
else
{
echo 'Wrong';
}
I've removed the A-Z (uppercase) from the regular expression since the i modifier is used.
I'd also like to explain what you did wrong in the example you provided.
First, by putting the ^ inside the square brackets ([]), you're essentially doing the opposite of what you were trying to do. Place a ^ inside the square brackets means "not including."
You were missing a *, + or ? at the end of the square bracket, unless you only wanted to match a single character. The * character means 0 or more, + means 1 or more and ? means 0 or 1.
The \W means any non-word character. That's probably not what you wanted.
Finally, to starting a regular expression with ^ means that the beginning of the string you're string to match must start with whatever is after the ^. Ending the regular expression with a $ means that the string must end with the characters preceding the $.
So by typing /^[a-z0-9_ ]+$/i you're saying match a string that starts with a-z0-9_ or a space, that contains at least of those characters (+) and ends.
PHP has a lot of documentation of the PCRE regular syntax which you can find here: http://ca2.php.net/manual/en/reference.pcre.pattern.syntax.php.
$message=$_GET['msg'];
if(preg_match('/^[a-zA-Z0-9_ ]+$/i',$message))
{
echo "Wrong";
}
else
{
echo "OK";
}