PHP regular expression for given condition [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regex , extract phone number from text/html
I have following requirement. There is a phone number field.
Its length should be between 8 and 10
It can contain only _ and white spaces
First and last elements should be digits
_ and white spaces should be in between 2 digits
Can I write a regular expression to validate the above things using php?
Example for valid entry: 231_901 347
Example for invalid entry: _123 567, 345__123
Also if possible, can you please write down the preg_match expression.
Here is the code which I tried:
$subject = "_012_345 69";
$pattern = '/[\d]{1}.[\d\s_]{5,10}.[\d]{1}$/';
$matches = preg_match($pattern, $subject);
echo($matches);
It should return 0 but it returns 1.

^(?=.{8,10}$)\d+(?:[ _]\d+)*$
Explanation:
^ # Start of string
(?=.{8,10}$) # Assert length 8-10
\d+ # One or more digits
(?: # followed by
[ _] # one space or underscore
\d+ # one or more digits
)* # zero or more times.
$ # End of string

Use regex pattern
^(?=.{8,10}$)(?=\d)(?=.*\d$)(?:\d|(?<=\d)[\s_](?=\d))+$
Explanation >>
^
Start if line/string
(?=.{8,10}$)
String size is 8-10 characters
(?=\d)
First character is digit
(?=.*\d$)
Last character is digit
(?:\d|(?<=\d)[\s_](?=\d))+
Allowed characters are (a) digits or (b) whitespace or underscore between digits
$
End of line/string

This meets all your requirements as I understand them:
^(?:\d[ _]*){7,9}\d$
As for those false positives you're getting, you just left off the start anchor (^). But even with anchor added it matches things like
1_________2
and
01234567890123
I'm assuming those are invalid because you meant there have to be eight to ten digits, not that the overall length should be eight to ten.

Related

Regular expression match for specific lower_case php validation [duplicate]

This question already has an answer here:
Javascript Regex restrict underscore at start and end
(1 answer)
Closed 4 months ago.
I need to compose a regular expression for string, with a max length of 6 characters, containing only Latin letters in lowercase, with an optional underscore separator, without underscore starting and trailing.
I tried the following
^[a-z_]{1,6}$
But it allows underscore at the start and the end.
I also tried:
^([a-z]_?[a-z]){1,6}$
^(([a-z]+)_?([a-z]+)){1,6}$
^([a-z](?:_?)[a-z]){1,6}$
But nothing works. Please help.
Expecting:
Valid:
ex_bar
Not valid:
_exbar
exbar_
_test_
This is a fairly simple pattern that should work ^(?!_)[a-z_]{0,5}[a-z]$. See here for a breakdown.
I would express your requirement as:
^(?!.{7,}$)[a-z](?:[a-z_]*[a-z])*$
This pattern matches:
^ from the start of the string
(?!.{7,}$) assert that at most 6 characters are present
[a-z] first letter must be a-z
(?:[a-z_]*[a-z])* match a-z or underscore in the middle, but only a-z at the end
$ end of the string
Note that the behavior of the above pattern is that one character matches must be only letter a-z. Similarly, two character matches can also only be a-z twice. With three character matches and longer, it is possible for underscore to appear in the middle.
Here is a running demo.
(?!^_)([a-z_]{6})(?<!_$)
You could use a negative look-ahead and negative look-behind to ensure that the string doesn't start and end with an _ underscore.
https://regex101.com/r/sMho0c/1

RegEx for matching specific HTML Entity pattern (Emoji)

I am working in this regular expression to match exactly the following pattern. The issue is that if it is exceeded, the pattern should not be considered:
I want exactly 6 digits starting with #, but if I write {5} returns true. Then the same happens with ; I want exactly one and to be at the end. Also, I don't know how to use here the $ to specify the final character.
if(preg_match(('/^(#)+([0-9]{6}){1}(;)/'),"#128515;")){
return true;
}
SHOULD BE IN THIS FORMAT:
#128515; for #DDDDDD; not ##DDDD;;
Exactly 6 digits start with one # and finish with one ;
preg_match will return 1 when it matches given subject and if you have 6 digits, it can match 5 as well when there is no ending semicolon as there is no ending boundary set.
You could add anchors ^ and $ to assert the start and the end of the string so it matches exactly 6 digits.
From your pattern you can omit {1} because the group is already matched 1 time.
If you don't reference to the groups in the code you could also omit them and just us a match only.
You could use:
^#[0-9]{6};$
^ Start of string
# Match #
[0-9]{6}; Match 6 digits
$ Assert end of string
Your code could look like
if(preg_match(('/^#[0-9]{6};$/'),"#128515;")){
return true;
}

explanation of preg_replace function in PHP [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
The preg_replace() function has so many possible values, like:
<?php
$patterns = array('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '/^\s*{(\w+)}\s*=/');
$replace = array('\3/\4/\1\2', '$\1 =');
echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27');
What does:
\3/\4/\1\2
And:
/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/','/^\s*{(\w+)}\s*=/
mean?
Is there any information available to help understand the meanings at one place? Any help or documents would be appreciated! Thanks in Advance.
Take a look at http://www.tutorialspoint.com/php/php_regular_expression.htm
\3 is the captured group 3
\4 is the captured group 4
...an so on...
\w means any word character.
\d means any digit.
\s means any white space.
+ means match the preceding pattern at least once or more.
* means match the preceding pattern 0 times or more.
{n,m} means match the preceding pattern at least n times to m times max.
{n} means match the preceding pattern exactly n times.
(n,} means match the preceding pattern at least n times or more.
(...) is a captured group.
So, the first thing to point out, is that we have an array of patterns ($patterns), and an array of replacements ($replace). Let's take each pattern and replacement and break it down:
Pattern:
/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/
Replacement:
\3/\4/\1\2
This takes a date and converts it from a YYYY-M-D format to a M/D/YYYY format. Let's break down it's components:
/ ... / # The starting and trailing slash mark the beginning and end of the expression.
(19|20) # Matches either 19 or 20, capturing the result as \1.
# \1 will be 19 or 20.
(\d{2}) # Matches any two digits (must be two digits), capturing the result as \2.
# \2 will be the two digits captured here.
- # Literal "-" character, not captured.
(\d{2}) # Either 1 or 2 digits, capturing the result as \3.
# \3 will be the one or two digits captured here.
- # Literal "-" character, not captured.
(\d{2}) # Either 1 or 2 digits, capturing the result as \4.
# \4 will be the one or two digits captured here.
This match is replaced by \3/\4/\1\2, which means:
\3 # The two digits captured in the 3rd set of `()`s, representing the month.
/ # A literal '/'.
\4 # The two digits captured in the 4rd set of `()`s, representing the day.
/ # A literal '/'.
\1 # Either '19' or '20'; the first two digits captured (first `()`s).
\2 # The two digits captured in the 2nd set of `()`s, representing the last two digits of the year.
Pattern:
/^\s*{(\w+)}\s*=/
Replacement:
$\1 =
This takes a variable name encoded as {variable} and converts it to $variable = <date>. Let's break it down:
/ ... / # The starting and trailing slash mark the beginning and end of the expression.
^ # Matches the beginning of the string, anchoring the match.
# If the following character isn't matched exactly at the beginning of the string, the expression won't match.
\s* # Any whitespace character. This can include spaces, tabs, etc.
# The '*' means "zero or more occurrences".
# So, the whitespace is optional, but there can be any amount of it at the beginning of the line.
{ # A literal '{' character.
(\w+) # Any 'word' character (a-z, A-Z, 0-9, _). This is captured in \1.
# \1 will be the text contained between the { and }, and is the only thing "captured" in this expression.
} # A literal '}' character.
\s* # Any whitespace character. This can include spaces, tabs, etc.
= # A literal '=' character.
This match is replaced by $\1 =, which means:
$ # A literal '$' character.
\1 # The text captured in the 1st and only set of `()`s, representing the variable name.
# A literal space.
= # A literal '=' character.
Lastly, I wanted to show you a couple of resources. The regex-format you're using is called "PCRE", or Perl-Compatible Regular Expressions. Here is a quick cheat-sheet on PCRE for PHP. Over the last few years, several tools have been popping up to help you visualize, explain, and test regular expressions. One is Regex 101 (just Google "regex tester" or "regex visualizer"). If you look here, this is an explanation of the first RegEx, and here is an explanation of the second. There are others as well, like Debuggex, Regex Tester, etc. But I find the detailed match breakdown on Regex 101 to be pretty useful.

PHP check that string has 2 numbers, 8 chars and 1 capital

I found lots of php regex and other options to determine string length, and if it contains one letter or one number, but how do I determine if a string has 2 numbers in it?
I am trying to validate a password that
Must have exactly 8 characters
One of them must be an Uppercase letter
2 of them must be numbers
Is there a one line regex solution for this?
if (preg_match(
'/^ # Start of string
(?=.*\p{Lu}) # at least one uppercase letter
(?=.*\d.*\d) # at least two digits
.{8} # exactly 8 characters
$ # End of string
/xu',
$subject)) {
# Successful match
(?=...) is a lookahead assertion. It checks if a certain regex can be matched at the current position, but doesn't actually consume any part of the string, so you can just place several of those in a row.

What does this regular expression mean? /^obj(\d+)\-{0,1}(|mi\d{0,1}|critical|questionText|answerText\-{0,1}\d+)$/ [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
What does this regular expression mean?
preg_match("/^obj(\d+)\-{0,1}(|mi\d{0,1}|critical|questionText|answerText\-{0,1}\d+)$/", $k, $a)
the preg_match is a php function that should translate it
This expression, commented would look like
^ // start of line
obj // literal obj
(\d+) // one or more digits (0-9), captured in a group
-{0,1} // optional dash
( // start second capturing group
// nothing
| // ... OR ...
mi\d{0,1} // literal mi, followed by an optional digit
| // ... OR ...
critical // literal critical
| // ... OR ...
questionText // literal questionText
| // ... OR ...
answerText // literal answerText
-{0,1} // optional dash
\d+ // one or more digits (0-9)
) // end of capturing group
$ // end of line
An example of what is matches would be
obj1000-critical or obj1000answerText-100
^obj(\d+)-{0,1} means that the string is the start of a line and begins with obj, followed by a number of at least 1 digit, then there might be a - sign.
(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) means that the text is one of the following:
nothing
mi which might be followed by a digit
critical
questionText
answerText which can be followed by one - sign, and after that a number of at least 1 digit
Then there's the end of the line. The search is case sensitive.
I'd firstly suggest finding a regex tutorial and reading up on how they are structured. This isn't a particularly complciated regex so you should be able to figure it out with some bookwork and it will mean you won't have to ask abotu the next regex you come across. ;-)
What the regex means broken down is as follows:
^ - matches the beginning of the string.
obj - this is literal text so will match those characters at the beginning of the string
(\d+) - this will match one or more digits (0-9) and the brackets will mean they are captured in such a way as they could be used after the parsing.
-{0,1} - this will match 0 or 1 "-" characters.
(|mi\d{0,1}|critical|questionText|answerText-{0,1}\d+) - again the brackets will capture this as a group. The "|" is used as an "or" so it will match any of the separated values. Although I'm not sure I think that the fact it starts with a "|" might mean it will match an empty string.
mi\d{0,1} - matches the miteral string mi followed by 0 or 1 digits.
critical, questionText - these are both literal options that match the exact text
answerText-{0,1}\d+ - this will match the literal string answerText followed by an optional "-" and one or more digits.
$ - the string must end immediately after the previous match.
I hope that makes sense to you. As I say, check some tutorials and docs if you need more help. :)
IT will match a string that starts with obj, followed by a numer, an optional dash, followed by either
nothing
mi followd by a number
critical
questionTest
answerText followed by an optional - which is in turn followed by a number (not optional)
and then the end of the string.
Examples:
obj932-mi21
obj3124critical
obj1-answerText-86
obj654answerText8
obj23-
It matches a certain format for lines that contain, for example:
obj1-mi5
obj2critical
obj3-questionText
obj4answerText-0
Do these patterns look familiar?
preg_match will find that pattern in $k and store matches in $a. Do the following to see what it found.
print_r($a);

Categories