How do I test if string maches integer:integer with preg_match? - php

I need a regular expression to test if string matches integer:integer (ex: 9:4).
I have tried
preg_match("[0-9]:[0-9]", $str)
but it's not correct.

You have to mark the start and end of the regular expression, usually with /.
Try this:
preg_match("/[0-9]:[0-9]/", $str)
One hint: you can use \d instead of [0-9].
If you want to make sure that the string only contains digit:digit, use ^ as the marker for the start of the string and $ for the end:
preg_match("/^[0-9]:[0-9]$/", $str)
Also, add + to match numbers of more than one digit:
preg_match("/^[0-9]+:[0-9]+$/", $str)

^[0-9](:[0-9])*$
^ matches the start of the string, and $ matches the end, ensuring that you're examining the entire string. It will match a single digit, plus zero or more instances of a colons followed by a digit after it.

Related

How to extract the last 2 delimitered numbers using regex

I have to extract the first instance of a number-number. For example I want to extract 8236497-234783 from the string bnjdfg/dfg.vom/fdgd3-8236497-234783/dfg8jfg.vofg. The string has no apparent structure besides the number followed by a dash and followed by a number which is the thing I want to extract.
The thing I want to extract may be at the very start of the string, or the middle, or the end, or maybe the entire string itself is just a number-number.
$b = "bnjdfg/dfg.vom/fdgd3-8236497-234783/dfg8jfg.vofg";
preg_match('\d-\d', $b, $matches);
echo($matches[0]);
// Expecting to print 8236497-234783
You're missing the delimiter around the regexp. PHP's preg functions require that the regex begin with a punctuation character, and it looks for the matching character at the end of the regexp (because flags can be put after the second delimiter).
\d just matches a single digit. If you want to match a string of digits, you should write \d+.
You should require that the numbers be surrounded by word boundaries with \b, otherwise it will match the 3 at the end of fdgd3
preg_match('/\b\d+-\d+\b/', $b, $matches);

check the value entered by the user with regular expression in php

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'])

php regex / preg_match

I'm trying to match the numbers inside ('')
$linkvar ="<a onclick="javascript:open('597967');" class="links">more</a>"
preg_match("^[0-9]$",$linkvar,$result);
Your regex only matches if the entire string is made up of one number because of the ^ and $ modifiers. Your current regex translates in human language to:
^ means "this is the start of the string"
[0-9] means "match a single numeric character"
$ means "this is the end of the string"
Change it to:
preg_match("[0-9]+",$linkvar,$result);
Or alternatively, the shorthand syntax for matching numbers:
preg_match("\d+",$linkvar,$result);
The + modifier means that "one or more" numbers must be found in order for it to be a match.
Additionally, if you want to actually capture the numbers inside the string you'll need to add parentheses to inform preg_match that you actually want to "save" the numbers.
Your regex will only match if the string is exactly one digit. To match only the digits inside the quotes, use:
preg_match("/'(\d+)'/", $linkvar, $result);
var_dump($result[1]);
The ^ and $ match the start and end of the string, which means you are currently searching for a string containing ONLY a single digit. Remove them and add a plus quantifier, leaving just "[0-9]+", and it will find the first group of digits in the string.
preg_match("[0-9]+",$linkvar,$result);

Regex to match numbers, # # % signs

I am trying to write a regex that matches all numbers (0-9) and # # % signs.
I have tried ^[0-9#%#]$ , it doesn't work.
I want it to match, for example: 1234345, 2323, 1, 3#, %#, 9, 23743, #####, or whatever...
There must be something missing?
Thank you
You're almost right... All you're missing is something to tell the regular expression there may be more than once of those characters like a * (0 or more) or a + (1 or more).
^[0-9#%#]+$
The ^ and $ are used do indicate the start and end of a string, respectively. Make sure that you string only contains those characters otherwise, it won't work (e.g. "The number is 89#1" wouldn't work because the string begins with something other than 0-9, #, %, or #).
Your pattern ^[0-9#%#]$ only matches strings that are one character long. The [] construct matches a single character, and the ^ and $ anchors mean that nothing can come before or after the character matched by the [].
If you just want to know if the string has one of those characters in it, then [0-9#%#] will do that. If you want to match a string that must have at least one character in it, then use ^[0-9#%#]+$. The "+" means to match one or more of the preceding item. If you also want to match empty strings, then use [0-9#%#]*. The "*" means to match zero or more of the preceding item.
It should be /^[0-9#%#]+$/. The + is a qualifier that means "one or more of the preceding".
The problem with your current regex is that it will only match one character that could either be a number or #, %, or #. This is because the ^ and $ characters match the beginning and the end of the line respectively. By adding the + qualifier, you are saying that you want to match one or more of the preceding character-class, and that the entire line consists of one or more of the characters in the specified character-class.
remove the caret (^), it is used to match from the start of the string.
You forgot "+"
^[0-9#%#]+$ must work

php regular expression for "|" and digits

I want know, what regular expression should I have for my string. My string can contains only "|" and digits.For example: "111|333|111|333". And string must begin from number. I am using this code, but he is ugly:
if (!preg_match('/\|d/', $ids)) {
$this->_redirect(ROOT_PATH . '/commission/payment/active');
}
Thank you in advance. Sorry for my english.
Looking at your example I assume you are looking for a regex to match string that begin and end with numbers and numbers are separated with |. If so you can use:
^\d+(?:\|\d+)*$
Explanation:
^ - Start anchor.
\d+ - One ore more digits, that is a number.
(? ) - Used for grouping.
\| - | is a regex meta char used for alternation,
to match a literal pipe, escape it.
* - Quantifier for zero or more.
$ - End anchor.
The regex is:
^\d[|\d]*$
^ - Start matching only from the beginning of the string
\d - Match a digit
[] - Define a class of possible matches. Match any of the following cases:
| - (inside a character class) Match the '|' character
\d - Match a digit
$ - End matching only from the beginning of the string
Note: Escaping the | is not necessary in this situation.
A string that contains only | or digits and begins with a digit is written as ^\d(\||\d)*$. That means: either \| (notice the escape!) or a digit, written as \d, multiple times.
The ^ and $ mean: from start to end, i.e. there’s no other character before or after that.
I think /^\d[\d\|]*$/ would work, however, if you always have three digits separated by bars, you need /^\d{3}(?:\|\d{3})*$/.
EDIT:
Finally, if you always have sequences of one or more number separated by bars, this will do: /^\d+(?:\|\d+)*$/.

Categories