I need a regular expression for string validation. String can be empty, can have 5 digits, and can have 9 digits. Other situations is invalid. I am using the next regex:
/\d{5}|\d{9}/
But it doesn't work.
Just as Marc B said in the comments, I would use this regular expression:
/^(\d{5}(\d{4})?)?$/
This matches either exactly five digits that might be followed by another four digits (thus nine digits in total) or no characters at all (note the ? quantifier around the digits expression that makes the group optional).
The advantage of this pattern in opposite to the other mentioned patterns with alternations is that this won’t require backtracking if matching five digits failed.
use anchors and "?" to allow empty string
/^(\d{5}|\d{9})?$/
~^(?:\d{5}|\d{9}|)$~
You forgot the anchors ^ and $. Without them the string would match those digits anywhere in the string, not only at beginning or end. Furthermore you didn't cover the empty string case.
"doesn't work" isn't much help. but wouldn't it be something like this?
/^(\d{5}|\d{9}|)$/
(Bit rusty on regexp, but i'm trying to do is "start, then 5 digits OR 9 digits OR nothing, then end)
The answer as to why it doesent work is with Perl style regex's alternations are prioritized from left to right.
Change it to:
/\d{9}|\d{5}/ (Though, this won't tell you anything else about 6-8 and 10-infinity
unless its anchored with assertions or something else.)
/^(\d{5}|\d{9}|)$/
Related
Is it possible to write a regular expression that matches with digits that does not have integer on left or right side? If we have these strings:
a20c
20c
.20c
a20-
120
It should match the four first, but not the last one.
This regex patterns will match text that has the 20 somewhere in the middle and of which no other number is touching the 20. This could occur anywhere in the text, therefore matching DM11 20-B but not DM1120-B.
[^0-9]20[^0-9]
Or a little more condensed:
\D20\D
You can use ^\D{1}\d+\D{1}$
Olso you can test your regex here:
https://regex101.com/r/nU4jL1/2
You can use 2 lookarounds to make sure your digits are surrounded by non-digits:
(?<=\D)\d+|\d+(?=\D)
RegEx Demo
I am curious about finding a regular expression for dollars. My inputs and rules are that there can only be digits 0 to 9 and an optional deciaml point. If the decimal exists, it must have two 0 t 9 digits after it.
So it can except:
1000
1000.99
But not:
10001.1
1000.
1,000
$100.9
Do you know anything about regular expressions?
Let me explain the solution:
1)you want digits, those are [0-9]
2)you want at least one of them, which is +
3)then there may be something, lets put it into brackets, 0 or 1 times means ?
so you have now this [0-9]+(something)?
4)now you want in something to be decimal point, dot is special char in regex so you have to escape it \.
5)then you want numbers again, exactly two of them which is {2}
Here you are, full expression:
$expression="/[0-9]+(\.[0-9]{2})?/";
Here you go:
/^[0-9]+(?:\.[0-9]{2}){0,1}$/
This will check arbitrary amounts:
/^(?:[0-9]{1,3})(?:,[0-9]{3})*(?:|\.[0-9]+)$/
E.g. it will validate:
1,432.33
342
1.2
0.343
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()
Quick regex question (since i am horrible at it)
I have a field that can only have either:
XXXXXXXXXX or XXXXXX-XXXX where X is a real number.
Bonus if the regex works well with PHP's regex functions.
The Answer:
Here's the code from RoBorg's answer, for those interested.
if(!preg_match("/^\d{6}-?\d{4}$/", $var))
{
// The entry didn't match
}
/^\d{6}-?\d{4}$/
That's
^ Start of string
\d{6} a digit, repeated exactly 6 times
-? an optional "-"
\d{4} a digit, repeated exactly 4 times
$ end of string
Just quickly, it'd be something like this:
\d{6}-?\d{4}
You may have to escape the hyphen in PHP.
If you want a specific number (the question wording was originally somewhat ambiguous), search for (e.g.):
^123456-?7890$
If searching for any 10 digit number with that format, search for:
^\d{6}-?\d{4}$
The ? qualifier after the dash means "0 or 1 occurrences of the preceding entity"