PHP preg_match range - php

I want to use preg_match to match numbers 1 - 21. How can I do this using preg_match? If the number is greater than 21 I don't want to match anything.
example preg_match('([0-9][0-1]{0,2})', 'Johnathan 21');

Copied from comment above:
I suggest matching simply ([0-9]{1,2}) (maybe wrapped in \b, based on input format) and filtering the numeric value later, in PHP code.
See also Raymond Chen's thoughts on the subject.

Literally:
preg_match('~ (1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21)$~', 'Johnathan 21');
But maybe this is more nifty:
preg_match('~ ([1-9]|1[0-9]|2[01])$~', 'Johnathan 21');

You could stablish an incremental filter like this example for TCP ports (altougth includes port number 0), like the previous answer:
preg_match('/^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/', '62000');

Related

preg_match_all for Unknown Sets of 3 Integers

I am using preg_match_all, but I have a problem I am not sure can be solved using this method. The following line is part of what I am retrieving:
XXC033-101-143-147-175-142115-
The sets of numbers (033-101-143, etc) are what I want to refer to. However, the number of sets (always containing three integers) is unknown and can range anywhere from 1 to 10. If I knew there would always only be 2 sets, I would have the following:
if (preg_match_all('#([A-Z]{2}C)([0-9]{3})-([0-9]{3})-([0-9]{6})#', $wwalist, $matches))
...rest of code...
Is there anyway to do this when I have no way of knowing the number of possible sets of 3 integers. They will always be between the #([A-Z]{2}C) and -([0-9]{6}).
Any help would be greatly appreciated! Thanks!
Use
'#([A-Z]{2}C)([0-9]{3}-){1,10}([0-9]{6})#'
{1,10} specifies that the preceding subpattern enclosed in brackets [0-9]{3}- will repeat 1-10 times.
In addition:
If it can repeat 0 or more times for an indefinite maximum number, use *.
If it can repeat 1 or more times for an indefinite maximum number, use +.
Targeting only the 3-digit substrings, individually/optionally capture the groups like this:
Pattern: (Demo)
/[A-Z]{2}C\K(\d{3}-)(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?(\d{3}-)?/

My regular expression is not being cool

My current one works almost perfectly but it misses out any other digits after a character like - or /.
The Original String is something like:
#!012 , #!02/09#!011 #!04-072
My current one works on stuff like:
$viewedResult = preg_replace('/#!([0-9A-Z]{1,4})/', '${1}', $viewedResult);
This would give me something like:
04-072<br />
but I want the "-072" in the Number2 bit like:
04-072
It could also be able to include /072s:
04/072
Any ideas? Remember that there is a #! in front of the number!
You could use something like:
preg_replace('~#!([0-9A-Z][0-9A-Z/-]{0,5})~', '${1}', $viewedResult);
I would split up the regular expression in two, as I think you want to match something like \d{1..4}[-/]?\d{1..4} where the first and second set of digits total 4. It's probably better just to look for (\d+[-/]/d+) and do a check afterwards if the total digits size is correct, e.g. finding all matches of (\d+) in the result, and programmatically check their length.
In general, I would pay close attention to what exactly is allowed input, and what (error) you will get if the input does not match. Splitting things up will make it much easier to show a correct error message, such as "number of digits in mooring spot incorrect" (which is I presume what this is about).
If you want the value of Number2, then use:
Number2\s*=\s*([^;"]*)[;"]
And then use the first capture group.
Stupid question: why not using this regular expression /#!([0-9A-Z/-]{1,4})/ ?
Side note: the {1,4} blocks you will retrieving the value 04-072 (it's 6 chars long).
Hope that helps :)

Conditional regex for URL formatting

Looking for an efficient regex to do this
/url-example-123.shtml & /url-example-25.shtml
to /url-example-3
Objectives:
remove the .shtml
replace with 3 if the numbers are between 25 and 125
Since you didn't give a language I can only give you a regex. You can use the following to match numbers between 25 & 125: (?:(?:1[0-1][0-9])|(?:12[0-5]))|(?:[^\d](?:2[5-9])|(?:[^\d](?:[3-9][0-9]))). You can then just do a replace with that match and a 3. If there will always be a .shrml at the end then you could add that to the end of the expression.
The question is vague. Not sure what should happen if there is no number, or if the number is outside the given range. Now that you've specified PHP, I've edited my answer:
echo preg_replace(
"/(?<=\/url-example-)(2[5-9]|[4-9][0-9]|1[01][0-9]|12[0-5]).shtml/",
"3",
myUrl);

Rotation in PHP's regex

How can you match the following words by PHP, either by regex/globbing/...?
Examples
INNO, heppeh, isi, pekkep, dadad, mum
My attempt would be to make a regex which has 3 parts:
1st match match [a-zA-Z]*
[a-zA-Z]?
rotation of the 1st match // Problem here!
The part 3 is the problem, since I do not know how to rotate the match.
This suggests me that regex is not the best solution here, since it is too very inefficient for long words.
I think regex are a bad solution. I'd do something with the condition like: ($word == strrev($word)).
Regexs are not suitable for finding palindromes of an arbitrary length.
However, if you are trying to find all of the palindromes in a large set of text, you could use regex to find a list of things that might be palindromes, and then filter that list to find the words that actually are palindromes.
For example, you can use a regex to find all words such that the first X characters are the reverse of the last X characters (from some small fixed value of X, like 2 or 3), and then run a secondary filter against all the matches to see if the whole word is in fact a palindrome.
In PHP once you get the string you want to check (by regex or split or whatever) you can just:
if ($string == strrev($string)) // it's a palindrome!
i think this regexp can work
$re = '~([a-z])(.?|(?R))\1~';

PHP: Simple regular expressions to match length?

I'm creating a registration system that needs to check the name/pass etc. with REGEX (and prefer to), what I've got so far is:
//Check so numbers aren't first, such as 00foobar
preg_match('/^(?!\d)[a-z0-9]+$/iD',$usrname);
//Just simple check
preg_match('/^[a-zA-Z0-9]+$/',$psword);
But I have to do stupid things in IF statements like:
if strlen($psword) > 30 || if (strlen($psword) < 4) ....
How would I impliment the length checking in my two original regular expression statements? This would make me so happy..
same but using the \w and \d for word and digits, but you might want also to include basic symbols like %!?/ ... etc...
preg_match('/^[\w\d]{4,30}$/',$psword);
the {n,v} would validate for minimum n and maximum v elements before.
like A{2,3} would validate for AA and AAA. you can take a look there for more references
On the same fashion if you want only to set the minimum of patern {n,} would do it. For example:
preg_match('/^[\w\d]{4,}$/',$psword);
I think this should do the trick:
preg_match('/^[a-zA-Z0-9]{4,30}$/',$psword);

Categories