REGEX Build for a sequence - php

I want to do the validation for the String "DDP-FA12-BSE-007" using regex. Any help will be appreciated :)
tHE string is ABC-DE12-FGH-345 or DE12-FGH-345
In first 3 places all the alphabets are Allowed.
Then a -
2nd place consist of two parts. For first two places only alphabets.
While for last two places only Digits are allowed.
Then a -
On 3rd place only two Alphabets are allowed.
Then a -
For forth place there should be only three Digits are allowed.
It should be like this DDP-FA12-BSE-007 or FA12-BSE-007
Ps: DE Should be FAor SP

Here you go:
^[A-Z]{3}-[A-Z]{2}\d{2}-[A-Z]{3}-\d{3}$
For little bit changes by yourself so must visit Regerx. So here you can see cheat code and it is very easy to develop regex by yourself. Hope it will help you.

The Answer is
^[A-Za-z]{3}-[A-Za-z]{2}\d{2}-[A-Za-z]{3}-\d{3}|^[A-Za-z]{2}\d{2}-[A-Za-z]{3}-\d{3}$
It will help to DO the verification for Upper an lowercase letters as well as
DDP-FA12-BSE-007 Or FA12-BSE-007
For online check this link
https://regex101.com/r/mJ9dH4/2

Related

Regex laravel validation

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+)?/

regex to find 3 instances of letter in a row php

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

Basic Regular Expression for

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]+?\.)+.+
/^(.+:\/\/)?[^.]+\.[^.\/]+([.\/][^.\/]+)*$/

No periods at beginning or end of expression?

I want to allow alphanumeric characters and periods; however, the phrase cannot contain more two or more periods in a row, it cannot start or end with a period, and spaces are not allowed.
I am using both PHP and Javascript.
So far, I have /^(?!.*\.{2})[a-zA-Z0-9.]+$/
This works for allowing alphanumeric characters and periods, while denying spaces and consecutive periods, but I still am not sure how to check for starting and/or ending periods. How might I do this? and, is there an even better way to do what I already have?
It nearly always helps to draw a finite state machine to conceptualize what your regular expression should look like.
^(?:\w\.?)*\w$
here's a possible way
/^(?!\.)((?:[a-z\d]|(?<!\.)\.)+)(?<!\.)$/i
for more explanations and tests see here: http://www.regex101.com/r/rZ6yH4
edit: according to tyler's solution, here's him way, shortened and reduced to letters and digits
/^(?:[a-z\d]+(?:\.(?!$))?)+$/i
( http://www.regex101.com/r/dL5aG0 )
A start would be:
/^[^. ](?!.*\.{2})[a-zA-Z0-9.]+[^. ]$/
but it should be tested carefully.

PHP: Validate string containing numbers, separated by hyphen (possible by preg_match)?

I’m trying to validate a string which contains numbers where each four numbers are separated by a hyphen, for example 1111-2222-3333-4444
I’m trying to do some kind of validating so I can guarantee that this format is being used (with 16 digits, three hyphens and nothing else). I’ve this preg_match where it checks for digits only but I need to accept hyphens and this format.
preg_match('/^[0-9]{1,}$/', $validatenumbers)
I’ve tried to do it with regex but unfortunately it isn’t my strongest side so I haven’t been able to correctly validate the numbers.
It is important that it is in PHP and not Javascript because of the ability to “turn off” javascript in a browser.
preg_match("/^([0-9]{4}-){3}[0-9]{4}$/", $input);
([0-9]{4}-){3} Matches exactly 3 groups of 4 digits followed by a hyphen. That is terminated by another group [0-9]{4} (4 digits without a hyphen).
preg_match('/^[0-9]{4}\-[0-9]{4}\-[0-9]{4}\-[0-9]{4}$/',$numbers);
i think that should work.
This looks like a credit card number. If that's the case, you should use a Luhn checksum instead of a simple regex.
try:
if(preg_match('#^\d{4}-\d{4}-\d{4}-\d{4}$#',$string){}
If you require to match that exact format the pattern would be '~^\d{4}-\d{4}-\d{4}-\d{4}$~', or you can write it more generally like this: '/^(\d+-)*\d+$/' (this would match 11, 11-11111... and so on),

Categories