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
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 would like to detect whether a user-chosen pin contains 4 identical numbers e.g 1111 or 2222. I'm using preg_match in PHP.
How can I adapt this answer to do this?
You could use this regex:
/(\d)\1{3}/
This matches a single digit (\d), and then matches that same digit 3 times \1{3}.
count(array_unique(str_split($pin))) > 1
Adapting from the answer you link to:
\b(\d)\1{3}\b
Instead of using \1+ that would match any number of repetitions of the first digit, you substitute it with \1{3} that will only allow three repetitions of the first digit, thus giving you the desired four digits when matched.
Or if you prefer:
\b(\d)\1\1\1\b
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 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}|)$/
I need to validate phone number in PHP. I have made following regex, but I also need to it to accept numbers starting with plus char, how can I do that?
^[0-9]$
The regex you provided in your question (^[0-9]$) will only match a one digit phone number.
At the very least you probably need to add the + modifier to allow one or more digits:
^[0-9]+$
To add an optional + at the beginning, you'll need to escape it since + is a special character in regular expressions:
^\+?[0-9]+$
And finally, \d is shorthand for [0-9] so you could shorten the expression to
^\+?\d+$
use this
/^(+\d)\s((\d{3})\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/;
Try this:
/^[\+]?([0-9]*)\s*\(?\s*([0-9]{3})?\s*\)?[\s\-\.]*([0-9]{3})[\s\-\.]*([0-9]{4})[a-zA-Z\s\,\.]*[x\#]*[a-zA-Z\.\s]*([\d]*)/
will match:
+1 ( 111)111-1111, #111
111-111-1111
(111)111-1111 x 1
111.111.1111x1
etc.