I am working with a third party API and they have certain rules for the passwords passed on to their system. One of them is the following:
Does not contain the same letter/number three or more times in a row. (e.g., aaa123would fail for three instances of "a" in a row, but a1a2a3 would pass).
I have tried looking for a solution but have not been able to find one. Could anybody help me with a solution to this. I am a php developer but not very clued up on creating a regex from scratch.
Any help would be much appreciated.
You need backreferences.
/(.)\1\1/
\1 means "whatever was captured in the first set of parentheses."
This should do what you need:
preg_match('/^(?!.*([a-z\d])\1{2})/i', $password)
See back references and assertions.
You can use the \1 reference to a ([a-zA-Z]) group:
/([a-zA-Z])\1{2}/
Demo
Related
Hello everyone I'm trying to validate if input has correct data. I need to check it input is number in pattern xx,xx or xxx. For example if user put 100 or 120,32 it will pass the validation. I'm trying to make regular expression for this but I'm not good in this topic. I have working code for xx,xx but how can I add to check first or second option to don't throw an error? Here is what I've got:
/^[+]?\d+\,\d+/
Your question
For the examples you mentioned, this would match all occurences:
/\d{2,3}(,\d{2})?)/
It says (in basic english): Two to three digits, followed by an optional "comma and two digits". For explanation, also see the example on Regex101: https://regex101.com/r/hU5kJ7/1
However I do not see any reason why you would limit the digits before the floating point to 3 digits, so to make it open, you could just leave out the 3:
/\d{2,}(,\d{2})?)/
It says (in basic english): Two to unlimited digits, followed by an optional "comma and two digits".
Apart from that
I would really not recommend implementing something that basic like number (or currency?) validation by yourself again. It is like reinventing the wheel. You will find many and many validation implementations like that all over the internet, in so called validation libraries or frameworks.
This regex should solve your problem:
/^\d+(,\d+)?/
I am trying to create a fairly simple regular expression to use with preg_match() used to check user agent strings for possible web crawlers/spiders.
For example, right now I am using something similar to this:
preg_match("/(bot|search|web|slurp|crawl)/i")
which seems to be successfully matching user agents that contain something like "googlebot" or "webcrawler".
However, the problem I am having is that this also matches when the user agent contains something as common as "webkit".
What modifications would be necessary to prevent specific words such as "webkit" from being matched? I have very little understanding of regular expressions and have spent hours trying various combinations based off answers to other questions and have had no success so far.
Many thanks in advance :)
In order to exclude a certain list of words, you can combine two lookaheads:
(?!webkit|robot)(?=bot|search|web|slurp|crawl)
Apparently the first part would be your exclusion list. This would match "web" but not "webkit"
A small note on the syntax. (?!regex) is negative lookahead and (?=regex) is a positive lookahead (non-consuming regular expression). You can read more upon it here.
In short, a lookahead means "match regex expr but after that continue matching at the original match-point."
For some reason I always get stuck making anything past extremely basic regular expressions.
I'm trying to make a regular expression that kind of looks like a URL. I only want basic checking.
I would like it to match the following patterns where X is "something".
X://X.X
X://X.X... etc.
X.X
X.X... etc
If the string contains one of these patterns, it is sufficient checking for me. This way a url like www.example.com:8888 will still match. I have tried many different REGEX combinations with preg_match and cannot seem to get any to behave the way I want it to. I have consulted many other related REGEX questions on SO but my readings have not helped me.
Any help? I will be happy to provide more information if you would like but I don't know what else you would need.
It takes practice but here is one that I made using a regex tester (http://www.regextester.com/) to check my pattern:
^.+(:\/\/|\.)([a-zA-Z0-9]+\.)+.+
My approach is to slowly build my pattern from the beginning and add on one piece at a time. This cheatsheet is extremely helpful for remembering http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/ what everything is.
Basically the pattern starts at the beginning of the string and checks for any characters followed by either :// or . then checks for groupings of letters and numbers followed by a . ending with any number of characters.
The pattern could probably be improved with groupings to not pass on invalid characters. But this one was quick and dirty. You could replace the first and last . with the characters that would be valid.
UPDATE
Per the comments here is an updated pattern:
^.+?(:\/\/|\.)?([a-zA-Z0-9]+?\.)+.+
/^(.+:\/\/)?[^.]+\.[^.\/]+([.\/][^.\/]+)*$/
I am trying to create a single regular expression that I can use to extract the number from two different urls in a PHP function. The format of these urls are:
/t/2121/title/
and
/top2121.html
I am bad at regular expressions and have already tried the following and many variants of it:
#^/t/(\d+?)/|/top(\d+?)\.html/#i
This is not doing anything and I am still at a complete loss after reading many sites and tutorials on regular expressions. Is there a regular expression I could create that would allow me to extra the number regardless of the url format entered?
Regex to extract only the digits while also checking if url matches accepted formats:
#^\/t(?:\/(\d+)\/[a-z_-]+\/?|op(\d+)\.html)$#i edit: captures in 2 groups
Explained demo here: http://regex101.com/r/dO5dI4
Variant #2: captures in the same group
#^\/t(?|\/(\d+)\/[a-z_-]+\/?$|op(\d+)\.html$)#i
Explained demo here: http://regex101.com/r/cG9vC3
if you just want the first digits after t regardless of the / between, something like this might work: #t/?(\d+)#i
edit:
example: http://codepad.viper-7.com/0z3ee0
I was able to get this regexp to match both types of url formats:
#^/(?:(?:t/)|(?:top))(\d+)(?:(?:\.html)|(?:/))#i
If anyone has a more efficient way of performing the same regexp, I would love to hear it.
If you got either one of these URL's you could use this expression. Your numbers should be stored in your second position:
#^/t(op|/)(\d+)(\.html|/.*)#i
Are there ever going to be numbers in the URL that you don't care about? If not, you can keep this simple by just capturing the numbers and ignoring the rest:
#(\d+)#
I just finished learning about regex and I thought that I should put it into something useful, so I created a small url routing script with php and the following regex:
^(?:/(\w+)?)*$
(the php code currently doesnt do anything, just prints out the matching groups from preg_match)
currently if given the url /foobar/foo/bar, the matching groups are the entire string (normal behavior) and the last part of the url (in this case: bar).
Obviously, this is a problem.
I think that this is caused because of the use of 1 capture group, which only captures the last matching string, but I'm not sure. any advice on the real cause of this and/or a solution to this will be greatly appreciated.
Thanks in advance!
You have diagnosed the problem correctly - on each repetition of the surrounding group, the previously matched contents of the capturing group are "overwritten" by the new match.
It's not quite clear what you would have expected to happen. I guess that you would have liked each part of the path to be "remembered" as its own group? This is something you can't do with repeated groups in PHP (only a few regex dialects (Perl 6 and .NET) allow something like this).
In your case, you're probably better off by using your regex to validate the URL and then split it along the slashes:
$result = preg_split('%/%', $subject);