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
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 need a regular expression which consist of: 1-3 digits and optional dot. It is something like IP pattern. I want my regex to allow the following:
192
192.
192.168
192.168.
and NOT the following:
192.1688
This is what I have so far:
preg_match('/^((\d{1,3})(\.?))+$/', $string);
But it still allows me to have more than 3 digits. Any suggestions how to fix the regex?
If you plan to match any number of 1-3 digit sequences separated with a dot (which is optional at the end), you can use
^\d{1,3}(?:\.\d{1,3})*\.?$
See demo
If you need the numbers to be in the range between 0 and 255 as in IP address, use
^(?:25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?))*\.?$
See another demo.
To limit to only 2 groups of numbers, use a ? quantifier with the second non-capturing group:
^(?:25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?))?\.?$
^
See the 3rd demo
My question is how do I modify the following regex to get the numbers from this string 160bhp, BUT also the numbers from the 90bhp, string?
For now, it takes only the 3 numbers and ignores the 2. I am trying with | without luck.
preg_match_all('/(\d{3})bhp\b,/', $str2b, $bhps);
Am guessing you may want to generalise this to match one or more digits before the bhp (with no upper limit)? If that's the case, use this:
preg_match_all('/(\d+)bhp\b,/', $str2b, $bhps);
Taking this a bit further, would zero or more whitespace characters be allowed between, e.g. "200 bhp"? If so, use this:
preg_match_all('/(\d+)\s*bhp\b,/', $str2b, $bhps);
And finally, does it need to be case insensitive, e.g. to allow 300BHP? If so, put an i after the final slash:
preg_match_all('/(\d+)\s*bhp\b,/i', $str2b, $bhps);
You simply change the value between the brackets to be {2,3}.
preg_match_all('/(\d{2,3})bhp\b,/', $str2b, $bhps);
This basically provides a minimum/maximum number of the preceding character definition that is to be matched.
\d{3} is saying there needs to be exactly 3 digits. Changing the {3} to * should solve the problem and allow you to retrieve any number of consecutive digits before the bhp.
preg_match_all('/(\d*)bhp\b,/', $str2b, $bhps);
regexpal is an excellent online regex tester.
preg_match_all('/(\d{2,3})bhp\b,/', $str2b, $bhps);
There's an additional quantifier that allows you to specify how many times a token can be repeated. The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite.
Source: http://www.regular-expressions.info/repeat.html
Bt if you want it to match also 1bhp and 160000bhp for example, use this instead:
preg_match_all('/(\d+)bhp\b,/', $str2b, $bhps);
Which will capture any number of digits on the left of bhp
In PHP how can I determine if a zip code exists within a string, and if so, return the zip code. In this context, a zip code can be either a string of 5 numbers (ie "55543") or a string of 5 numbers connected to 4 more numbers with a hyphen (ie "74657-9993")..
Can anyone help me out with a Regex pattern I can use with preg_match or any other good ways of doing this?
I have preg_match_all("/\d{5}/", $str, $matches); so far, but that doesn't account for the possible second 4 digits or the hyphen.
5 number connected to 4 more numbers with a hyphen:
preg_match_all("/\b\d{5}(?:-\d{4})?\b/", $str, $matches);
(?:-\d{4,})? is an optional group, with a hyphen, and at least 4 digits after it.
Edit: Forgot to prevent longer than 5 digits for the first part (and 4 digits for the second part), using a word boundary.
EDIT2:
Okay, something else I just noticed is that if you have 12345-12345 but don't want to get any number form this, you would use:
preg_match_all("/\b\d{5}(?!-\d{1,3}\b|-\d{5})(?:-\d{4})?\b/", $str, $matches);
The negative lookahead prevents the match of -12345 (or more digits, or less than 4 digits) if present, but allow only 4 digits ahead.
regex101 demo
Your pattern is
\b\d{5}(?:-\d{4})?\b
See it here on Regexr.
An important part here are the word boundaries \b, they ensure that not a part of a number is matched.
\d{5} is matching 5 digits as you already had it
(?:-\d{4})? is the optional part (because of the ? after the the group). The ?: at the start of the group is just making the group non-capturing.
I have recently implemented this in javascript
/^(\s*|\d{5}([\-]\d{4})?)$/;
Just modify your regex to allow the optional prefix:
preg_match_all("/\d{5}(\-\d{4})?/", $str, $matches);
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