i have string like p88t9014-name here is p is for Product and t for sub product id
and after - the name is user defined any name.
i try to match string with preg_match with this code ::
$name="p88t0056-name";
if(preg_match('/p[0-9]t[0-9]-[A-Z,a-z]/',$name,$match)) {
echo "yes";
} else {
echo "No";
}
print_r($m);
i just try to match formate if is this with format p[number]t[number]-[anystring]. but my code is not working.
You need to put quantifiers after your character classes:
'/p[0-9]+t[0-9]+-[A-Za-z]+/'
This regex will work for you:
/p(\d+)t(\d+)-(\w+)/g
Demo
Explaination:
p matches letter p
\d+ matches numbers 0-9
t matches letter t
\d+ matches numbers 0-9
- matches dash '-'
\W+ match any word character [a-zA-Z0-9_]
and g to catch all matches.
I am also not an expert on regex but trying multiple options on http://www.regex101.com helps as it shows explanations of characters in right side panel. Hope it helps in future :)
If you're always going to have a set number of digits, you can also use:
/^p[0-9]{2}t[0-9]{4}-[A-Za-z]+$/
Here's an example on RegExr: http://www.regexr.com/390ds
in regex [0-9] matches exactly one character, and so does [A-Z,a-z], and therefore $name is not match the pattern you give. Strings like "p8t0-A" and "p0t2," pass the test.
Besides, another problem in your pattern is that: [A-Z,a-z] matches not only alphabets but also , (a single comma). I guess the pattern you need is p[0-9]+t[0-9]+-[A-Za-z]+, in which +s represent "occurs at least once".
Related
I'm trying to make sure that when the user registers they use a specific series of characters. In my case i want them to enter something like "d1111111" so I want it to be 8 characters in total and to start with the letter d lowercase or uppercase, to allow the other 7 characters to be a number between 0 and 9. All help appreciated, thanks.
here's my code so far
$regex = '/[d][0-9]{8}/';
if(preg_match($regex, $username)){
echo "Valid";
} else {
echo "not a valid username";
}
Use for example
$regex = '/^[dD][0-9]{7}$/';
or
$regex = '/^d\d{7}$/i';
A character class […] matches a single character, [dD] therefore matches the letter d or D.
Alternatively, you can use the i option (also referred to as modifier) at the end of the expression, to let your regular expression match case-insensitively. It is also possible to change the mode inside the expression, I personally have never used that, though.
\d matches any digit and is the same as [0-9]. {7} denotes how many digits to match.
You need ^ and $ to ensure that the regular expression matches the whole username.
For PHP in particular, see PHP's PCRE docs.
Your regex is wrong here. First, there's no need of a character class to capture d. Just a literal d along with start-of-the-string anchor ^.
Now we want the remaining 7 letters to be digits. So it would be following with the end-of-the-string anchor $
$regex = '/^d\d{7}$/'
I'm trying to create a pattern in PHP that matches 2 or more upper case characters in a string.
I've tried the following, but it only matches 2 or more upper case characters in a row, not the entire string:
preg_match('/[A-Z]{2,}/', $string);
For example, the string "aBcDe" or "Red Apple" should return true.
You just have to allow other characters between your uppercase letters:
^(?:.*?\p{Lu}){2}
Demo
I used \p{Lu} here to include Unicode characters as well. If you don't want that just use [A-Z] instead like you did in your pattern.
This simply means:
^ from the start of the pattern
(?: group:
.*? match anything, but as few chars as possible
\p{Lu} match an uppercase letter
){2} ... two times
If all you need to do is identify that a string contains at least 2 uppercase characters then you can use the following:
[A-Z].*?[A-Z]
Try it here.
If you need to identify the specific uppercase characters in the string then things get more complicated.
UPDATE: As Lucas mentioned, you need a different regex if you want unicode support.
\p{Lu}.*?\p{Lu}
^.*[A-Z].*[A-Z].*$
A simple pattern stating the same would do.See demo.
https://regex101.com/r/pT4tM5/23
[A-Z].*[A-Z]
is about as simple as it gets - match an uppercase followed by anything repeated any number of times followed by any other uppercase letter.
If you need to match the whole line/string that has at least 2 upper case letters, you can also use
^(?=(?:.*[A-Z]){2}).+$
Demo here.
I am writing a script that needs to download images related to a product ID array to an external website.
Here are the possible product ID combinations.
ABC1234AB
ABC1234AB-CD
ABC1234AB-CDE
ABC1234ABC
I need to be able to convert them to their URL equivalent on the manufacturer's website, which are (In the same order):
abc1234_ab
abc1234_ab_cd
abc1234_ab_cde
abc1234_abc
I am looking for a Regex to use with preg_replace that would do the trick.
Thanks in advance!
$output = strtolower(preg_replace('~\d\K(?=[A-Z])|-~', '_', $input));
\K removes that is matched on the left from the match result, so , the digit before the letter is not a part of the match and will not be replaced.
(?=...) is a lookahead assertion that checks if a letter if following, it isn't a part of the match result too and will not be replaced too.
I'm a noob in regular expressions but I`ll give it a shot.
Input: /([A-Z]+)\d+([A-Z]+)\-([A-Z]+)/
A-Z matches uppercase alpha characters
\d matches numbers
"+" is used to repeat
And in the replacement callback use strtolower on the matches and join them how you want :P
I'm playing around with PHP Regex in order to improve my skills with it.
I'm having a hard time trying to understand the plus sign - so I wrote the following code:
$subject = 'aaa bbb cccc dddd';
echo preg_replace('/(\w)/',"$1*",$subject) . '<br>';
echo preg_replace('/(\w+)/',"$1*",$subject) . '<br>';
echo preg_replace('/(\w)+/',"$1*",$subject) . '<br>';
With results in:
a*a*a* b*b*b* c*c*c*c* d*d*d*d*
aaa* bbb* cccc* dddd*
a* b* c* d*
I don't understand why these results come about. Can someone please explain what's going on in this example
in regular expressions, + means one or more of the preceding character or group.
The pattern /(\w)/, means match a single word character (a-zA-Z0-9_) in a single group. So it will match each letter. The first match group will be just a. The replace will replace each individual letter with that letter followed by an asterisk.
The pattern /(\w+)/ will match one or more word characters in a group. So it will match each block of letters. The first match group will be aaa. The replace will replace each block of multiple letters followed by a asterisk.
The last pattern /(\w)+/ is a little more tricky, but will match a single word character in a group but the + means that it will match one or more of the group. So the first match will be a, but the replace will replace all of the groups until there isn't a match with the last matched group (of course followed by an asterisk). So if you tried the string aaab ccc, your result would end up as b* c*. b is the last matched group in the first sequence and so the replace would use that.
Your mistake isn't the plus sign, it's understanding what the parentesis is for and how it works. The parenthesis is for grouping your match into a variable, hence why you can do $1, the second set of () gives you $2 and so on...
(\w) means 1 word character
(\w+) means 1 or more word characters
(\w)+ matches 1 or more word characters, but only the first one is put into the variable, because only the \w is inside the paranthesis
get_by_my_column
If I only want to match the get_by portion of the above string, how can I do this? I keep reading on this regex cheatsheet that I should use \n but I can't figure out how to implement it properly...
I've tried variations of the following...
/((_){2})/
/(_+){2}/
/(\w+?_\w+?)_\w+/ (use non greedy quantifiers, your substring should be in capture group 1)
or just /\w+?_\w+?/ <---(edit: won't work, you do need that second underscore as regex structure to force the non greedy \w up to it :])
Do you need to use a regex for this? You could use explode() and just grab the first two elements of the resulting array.
Try
preg_match('/(^[a-z]+[_][a-z]+)/', $string, $results);
This matches a string that starts with a group of letters followed by an underscore followed by another set of letters.
Edit: (lowercase letters)
try /^get_by. ^ for the condition that g must be the starting character.