I have an html form with an input for a sales order number which should have the format of K1234/5678. It should always start with the letter K then 4 numbers, a / and followed by another set of 4 numbers.
I'm trying to validate the formatting using preg_match and I'm getting lost in the syntax of preg_match. From http://php.net/manual/en/function.preg-match.php I've gotten close. With the following code I'm able to verify that it contains at least 1 letter, some numbers and at least 1 non- alphanumeric value.
$so= $_POST['so'];
if (preg_match(""/^(?=.*[a-z]{1})(?=.*[0-9]{4})(?=.*[^a-z0-9]{1})/i", $so))
{
print $so;
}
What is the correct syntax to use for this? Is preg_match even the best way to do this?
Try this:
preg_match("#^K[0-9]{4}/[0-9]{4}$#i", $so)
Explanation:
The # characters are regular expression delimiters - they indicate the start/end of the pattern. The ^ and $ indicate the start and end of the string - this means that it will only match if your sales order number is the only thing in the string. The letter K means match that letter, [0-9]{4} means match a digit exactly 4 times. The i at the end means a case-insensitive match - the K will match either "K" or "k".
When developing regular expressions, I often use regular expression testers - these allow you to enter your data and try a bunch of different things to refine your regex. Google PHP regex tester to find a list of tools. Also, there's a very complete reference to regular expressions at http://www.regular-expressions.info/.
Related
I need to validate a regex where between STRING_{here}G_ can be 0 or even 4 digits, I tried the following regex:
(?<=TEST_[0-9]{0,4}G_).*
But the tester returns the error:
Your pattern contains one or more errors, please see the explanation section above.
And when trying to use manually, using two [0-9], it doesn't get my strings
ABC_TEST_20G_a123-abc1
ABC_TEST_100G_abc1
I need a regex that validates both strings and returns what is after G_
Remembering that the regex must have the "TEST_", it is a string that I need to validate
Most regexp engines don't allow lookbehinds to be variable-length, so you can't have a {0,4} quantifier in it.
Instead of a lookbehind, use a capture group to capture everything after this pattern.
TEST_[0-9]{0,4}G_(.*)
Capture group 1 will contain what you want to get.
DEMO
I have this few strings:
'user/1/myid_print'
'user/2/myid_print'
'user/3/myid_print'
'user/4/myid_print'
'user/5/myid_print'
The second part is the dynamic one which must contain only integers. What is it's regular expression?
Try this:
/user\/\d+\/myid_print/
the \d+ matches a number which contains at least one digit.
if it is a non-zero number, replace the \d+ with [1-9]\d*
It depends a bit on what language you're using, but one valid answer for Python is:
user/\d/myid_print
The / character is often used in describing regular expressions and sometimes needs \ before it to make it match part of the string, a valid answer might be:
user\/\d\/myid_print
These match the text "user/" and "/myid_print" literally, and \d matches the pattern "any digit 0-9". If you need to match the numbers 1,2,3,4,5 only, then use [1-5] instead of \d
Test it here: https://regex101.com/r/mS4xN4/1
I have to create regex to match ugly abbreviations and numbers. These can be one of following "formats":
1) [any alphabet char length of 1 char][0-9]
2) [double][whitespace][2-3 length of any alphabet char]
I tried to match double:
preg_match("/^-?(?:\d+|\d*\.\d+)$/", $source, $matches);
But I coldn't get it to select following example: 1.1 AA My test title. What is wrong with my regex and how can I add those others to my regex too?
In your regex you say "start of string, followed by maybe a - followed by at least one digit or followed by 0 or more digits, followed by a dot and followed by at least one digit and followed by the end of string.
So you regex could match for example.. 4.5, -.1 etc. This is exactly what you tell it to do.
You test input string does not match since there are other characters present after the number 1.1 and even if it somehow magically matched your "double" matching regex is wrong.
For a double without scientific notation you usually use this regex :
[-+]?\b[0-9]+(\.[0-9]+)?\b
Now that we have this out of our way we need a whitespace \s and
[2-3 length of alphabet]
Now I have no idea what [2-3 length of alphabet] means but by combining the above you get a regex like this :
[-+]?\b[0-9]+(\.[0-9]+)?\b\s[2-3 length of alphabet]
You can also place anchors ^$ if you want the string to match entirely :
^[-+]?\b[0-9]+(\.[0-9]+)?\b\s[2-3 length of alphabet]$
Feel free to ask if you are stuck! :)
I see multiple issues with your regex:
You try to match the whole string (as a number) by the anchors: ^ at the beginning and $ at the end. If you don't want that, remove those.
The number group is non-catching. It will be checked for matches, but those won't be added to $matches. That's because of the ?: internal options you set in (?:...). Remove ?: to make that group catching.
You place the shorter digit-pattern before the longer one. If you swap the order, the regex engine will look for it first and on success prefer it over the shorter one.
Maybe this already solves your issue:
preg_match("/-?(\d*\.\d+|\d+)/", $source, $matches);
Demo
I am trying to construct a regular expression for a string which can have 0 upto 4 characters. The characters can only be 0 to 9 or a to z or A to Z.
I have the following expression, it works but I dont know how to set it so that only maximum of 4 characters are accepted. In this expression, 0 to infinity characters that match the pattern are accepted.
'([0-9a-zA-Z\s]*)'
You can use {0,4} instead of the * which will allow zero to four instances of the preceding token:
'([0-9a-zA-Z\s]{0,4})'
(* is actually the same as {0,}, i.e. at least zero and unbounded.)
If you want to match a string that consists entirely of zero to four of those characters, you need to anchor the regex at both ends:
'(^[0-9a-zA-Z]{0,4}$)'
I took the liberty of removing the \s because it doesn't fit your problem description. Also, I don't know if you're aware of this, but those parentheses do not form a group, capturing or otherwise. They're not even part of the regex; PHP is using them as regex delimiters. Your regex is equivalent to:
'/^[0-9a-zA-Z]{0,4}$/'
If you really want to capture the whole match in group #1, you should add parentheses inside the delimiters:
'/(^[0-9a-zA-Z]{0,4}$)/'
... but I don't see why you would want to; the whole match is always captured in group #0 automatically.
You can use { } to specify finite quantifiers:
[0-9a-zA-Z\s]{0,4}
http://www.regular-expressions.info/reference.html
You can avoid regular expressions completely.
if (strlen($str) <= 4 && ctype_alnum($str)) {
// contains 0-4 characters, that are either letters or digits
}
ctype_alnum()
I use this preg_match condition for matching positive, negative and decimal values
/^[0-9,-\.]{1,50}$/
But when I enter --34.000 it does not show error, when I enter 34...9868 it does not show error,
what I want is that it must accept only positive, negative and decimal values.
Better if you use something like is_numeric() if yuo need to check if it's a number.
And your regex is totally broke because as now it can accept even only a string containing 50 dots
As yes123 stated, there are better ways to detect if a given input string is a numeric value. If you'd like to stick to regular expressions, the following might be OK for you:
/^-?[0-9]+(?:\.[0-9]+)?$/
Explanation:
match start of the string ( ^)
match a possible - character (-?); the ? means "not required"
match at least one number ([0-9]+)
possibly match the whole statement in the parentheses ((?:...)?); ?: means "do not capture the subpattern"
a point (\.); the . needs to be escaped due to its special function
at least one number ([0-9]+)
match end of the string ($)
You need to split up your regular expression so that it only accepts the characters in the right places. For example:
/^[+\-]?([0-9]+,)*[0-9]+(\.[0-9]+)?$/
To explain this expression:
[+\-]?: This checks for a + or - prefix to the number. It's completely optional, but can only be a + or -.
([0-9]+,)*: This allows an optional set of comma-delimited numbers. This is for the thousands, millions etc.
[0-9]+: This requires that the value contains at least some numbers
(\.[0-9]+)?: Finally, this allows an optional decimal point with trailing numbers.
try this regex ^-?\d*\.?\d+$
i suppose it however cannot be limited to 50 chars