Hi i have a problem with preg_match function.
i want to check whole text against pattern and return true if whole text matched with pattern and false for not matched or partially matched with pattern but i can't do this with php preg_match function.
for example i use bellow func to check if input is digit or no.
if(preg_match("/[\d]/","99ab") return true; else return false;
above code return true but i want to this return false.
and other regex that i want to check is users first name and last name with preg_match.
and another regex for phone numbers with this format (000) 000-0000.
please help me to solve this problem.
Just change the pattern like this:
"/^[\d]+$/"
Remember that:
^: means the start of the string
$: means the end of the string
You need to use ^ to indicate the start of the string and $ for the end
if (preg_match("/^[\d]+$/","99ab")) return true;
else return false;
Related
I need regular expression which matches a URL and return the desired value
Example (if the URL matches to)
1. http://example.com/amp
2. http://example.com/amp/
3. http://example.com/amp~
THEN
it should return: ?amp=1
ELSE
it should return: false
You should be able to use preg_replace to append ?amp= to the end of a matching string. Its functionality already does the if/else functional you require,
If matches are found, the new subject will be returned, otherwise subject will be returned unchanged or NULL if an error occurred.
(or I misread the it should return noting)
-http://php.net/manual/en/function.preg-replace.php
Something like
amp\K( |\/|~)$
Should do it
$string = 'http://example.com/amp~';
echo preg_replace('/amp\K( |\/|~)$/', '$1?amp=1', $string);
The $1 is optional, not sure if you wanted the found character included or not.
PHP Demo: https://eval.in/780432
Regex demo: https://regex101.com/r/JgcrLu/1/
$ is the end of the string. () is a capturing and alteration group. |s are alterations. \K skips the previously matched regex part.
You didn't specify the programming language you're using but you probably need something like:
php:
$new = preg_replace('%/amp\b(?:/|~)?%si', '/?amp=1', $old);
python:
new_string = re.sub(r"/amp\b(?:/|~)?", "/?amp=1", old_string, 0, re.IGNORECASE)
Regex Demo
This is my code:
$str = "sdf13631389945fssx6221363138994523213af";
preg_match_all("/^1\d{10}$/",$str,$result);
var_dump($result);
I want to match the phone no. 13631389945 but don't want to match this phone no. in 6221363138994523213 , so I write this, but it return empty, can you help me write a right pattern, thanks a lot!
It doesn't return a result because the whole string doesn't match the expression. If you only want part of the string to match then get rid of the ^ and $.
preg_match_all("/1\d{10}/",$str,$result);
To not match the phone number in the second part of the string you are going to have to explain what exactly makes it an invalid match. If it's the surrounding numbers then use this:
(?:^|[^\d])(1\d{10})(?:$|[^\d])
I have the next code and I'm trying to know if the string its valid based on the regular expression.
I'm trying to validate only strings that follow the next sequence.
lettersOrNumbersAndunderDashes=lettersOrNumbersAndUnderdashes
But that sequence can be repeated if there is a vertical bar.
For example parameter1=value1|parameter2=value2|parameterN=valueN
if (preg_match("/((^[A-Za-z0-9_]+=[A-Za-z0-9_]+)\|?)/m", "perPd_asd=as_3_4d|asdas=asdasd"))
return 'Valid';
return 'Invalid';
I think I'm missing something or building a wrong regular expression.
The wrong thing you did is putting a ^ at the beginning of the pattern, which means that it will only match if the text is at the beginning of the string. This should solve :
if (preg_match("/(([A-Za-z0-9_]+=[A-Za-z0-9_]+)\|?)/m", "perPd_asd=as_3_4d|asdas=asdasd"))
return 'Valid';
return 'Invalid';
It is possible that parameter name starts from number?
You need more test cases for your regular expression, for example:
0=somevalue
param=value|
one_more_param=##$%^|some_param=some-value
_=VALUE|abc=***
a=1|b=2|c=3
param=0|param=1
my solution is:
^(([_A-Za-z][A-Za-z0-9_]*=[^\|=]+)\|)*([_A-Za-z][A-Za-z0-9_]*=[^\|=]+)$
i'm trying to create a regex pattern that returns true if a certain word is not found. Ive tried using [^word] but that doesn't match up against a word just the individual characters as they appear.
I need preg_match(using php) to return true cause there are other words that I to match and return true.
if you are looking for a string within a string (no pattern needed) then use strstr() or stristr()
if (!preg_match("/word/", $string)) { // <-- true, if not match
// code here
}
In PHP, how do I check if a String contains only letters? I want to write an if statement that will return false if there is (white space, number, symbol) or anything else other than a-z and A-Z.
My string must contain ONLY letters.
I thought I could do it this way, but I'm doing it wrong:
if( ereg("[a-zA-Z]+", $myString))
return true;
else
return false;
How do I find out if myString contains only letters?
Yeah this works fine. Thanks
if(myString.matches("^[a-zA-Z]+$"))
Never heard of ereg, but I'd guess that it will match on substrings.
In that case, you want to include anchors on either end of your regexp so as to force a match on the whole string:
"^[a-zA-Z]+$"
Also, you could simplify your function to read
return ereg("^[a-zA-Z]+$", $myString);
because the if to return true or false from what's already a boolean is redundant.
Alternatively, you could match on any character that's not a letter, and return the complement of the result:
return !ereg("[^a-zA-Z]", $myString);
Note the ^ at the beginning of the character set, which inverts it. Also note that you no longer need the + after it, as a single "bad" character will cause a match.
Finally... this advice is for Java because you have a Java tag on your question. But the $ in $myString makes it look like you're dealing with, maybe Perl or PHP? Some clarification might help.
Your code looks like PHP. It would return true if the string has a letter in it. To make sure the string has only letters you need to use the start and end anchors:
In Java you can make use of the matches method of the String class:
boolean hasOnlyLetters(String str) {
return str.matches("^[a-zA-Z]+$");
}
In PHP the function ereg is deprecated now. You need to use the preg_match as replacement. The PHP equivalent of the above function is:
function hasOnlyLetters($str) {
return preg_match('/^[a-z]+$/i',$str);
}
I'm going to be different and use Character.isLetter definition of what is a letter.
if (myString.matches("\\p{javaLetter}*"))
Note that this matches more than just [A-Za-z]*.
A character is considered to be a letter if its general category type, provided by Character.getType(ch), is any of the following: UPPERCASE_LETTER, LOWERCASE_LETTER, TITLECASE_LETTER, MODIFIER_LETTER, OTHER_LETTER
Not all letters have case. Many characters are letters but are neither uppercase nor lowercase nor titlecase.
The \p{javaXXX} character classes is defined in Pattern API.
Alternatively, try checking if it contains anything other than letters: [^A-Za-z]
The easiest way to do a "is ALL characters of a given type" is to check if ANY character is NOT of the type.
So if \W denotes a non-character, then just check for one of those.