I wanted to use regular expression to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). I know I can use preg to do this, and so far I have this:
preg_match('/[A-Za-z0-9]/i', $string)
I am unsure however on how to limit it only to 8 digits/character scheme.
For exactly 8 char word you will need to use word boundaries: \b
preg_match('/\b[A-Z\d]{8}\b/i', $string)
Try
preg_match('/\b([A-Z0-9]{8})\b/i', $string)
The {8} matches exactly 8 times. I added the capturing group (the parentheses), in case you needed to extract the actual match.
You can also use {min,max} to match the pattern repeating between min and max times (inclusive, I think). Or you can leave one of the parameters out to leave it open ended. Eg {min,} to match at least min times
[a-zA-Z0-9] - will match upper or lowercase letters or numbers
{8} - will specify to match 8 of the preceeding token
put it together:
preg_match('/([A-Za-z0-9]{8})/i', $string)
example
Related
I want to extract an 8 digit number from a string using regex.
Test strings are:
hi 82799162
236232 (82342450)
test data 8979
Required respective output should be
82799162
82342450
null
I have tried following code:
preg_match('/[0-9]{8}$/', $string, $match);
preg_match('/\d{8}$/', $string, $match);
But neither retrieves the number from 236232 (82342450).
If a regex is to capture exactly 8 digits, is must contain:
\d{8} as a central part,
a "before" condition, ensuring that no digit occurs before your match,
an "after" condition, ensuring that no digit occurs after your match.
One of possible solutions is to use negative lookbehind / lookahead:
(?<!\d)\d{8}(?!\d)
Another option is word boundary assertions (at both ends):
\b\d{8}\b
I think, regex like [0-9]{8} is not enough, as it captures also
first 8 digits from a longer sequence of digits.
Are you happy with that?
The problem is with your $ sign, and it is used to indicate the end of your expression. So basically, with that expression, you are looking for a string which ends with a 8 digit number. But in your second test string; '236232 (82342450)', ends with a bracket, and therefore it doesn't match the criteria (does not end with a number).
So remove the trailing $ and it will work.
preg_match('/[0-9]{8}/',$string,$match);
Hope it helps!!
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 currently have this regular expression:
/(^| )[a-z]{5}-[a-z]{5}( |$)/i
start of string or space, 5 letters, literal dash, 5 letters, space or end of string, case insensitive.
This finds a string that looks like this: pejnd-zxdgn
I need to allow the first letter only to be a digit instead of a letter.
How do I write this?
Edit:
To clarify
should match: pejnd-zxdgn or 7ejnd-zxdgn
Should not match 7pejnd-zxdgn or 7ejn-zxdgn or p7ejnd-zxdgn
Just add a pattern for digit before the [a-z] part. And change the quantifier to {4}:
/(^| )[0-9][a-z]{4}-[0-9][a-z]{4}( |$)/i
After your update, you just want the first character to be digit or character. Also, you can use word boundaries - \b at the beginning and the end, as noted in comments. So, change your regex to:
/\b[a-z0-9][a-z]{4}-[a-z]{5}\b/i
I want to write php regular expression to find uppercase string , which can also contain one number and spaces, from text.
For example from this text "some text to contain EXAM PL E 7STRING uppercase word" I want to get string- EXAM PL E 7STRING ,
found string should start and end only with uppercase, but in the middle, without uppercase letters can also contain(but not necessarily ) one number and spaces. So, regex should match any of these patterns
1) EXAMPLESTRING - just uppercase string
2) EXAMP4LESTRING - with number
3) EXAMPLES TRING - with space
4) EXAM PL E STRING - with more than one spaces
5) EXAMP LE4STRING - with number and space
6) EXAMP LE 4ST RI NG - with number and spaces
and with total length string should be equal or more than 4 letters
I wrote this regex '/[A-Z]{1,}([A-Z\s]{2,}|\d?)[A-Z]{1,}/', that can find first 4 patterns, but I can not figure it out to match also the last 2 patterns.
Thanks
There is a neat trick called a lookahead. It just checks what is following after the current position. That can be used to check for multiple conditions:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])(?!(?:[A-Z\s]*\d){2})[A-Z][A-Z\s\d]*[A-Z]/'
The first lookaround is actually a lookbehind and checks that there is no previous uppercase letter. This is just a little speedup for strings that would fail the match anyway. The second lookaround (a lookahead) checks that there are at least four letters. The third one checks that there are no two digits. The rest just matches then a string of the allowed characters, starting and ending with an uppercase letter.
Note that in the case of two digits this will not match at all (instead of matching everything up to the second digit). If you do want to match in such a case, you could incorporate the "1 digit" rule into the actual match instead:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])[A-Z][A-Z\s]*\d?[A-Z\s]*[A-Z]/'
EDIT:
As Ωmega pointed out, this will cause problems if there are less then four letters before the second digit, but more after that. This is actually quite tough, because the assertion needs to be, that there are more than 4 letters before the second digit. Since we do not know where the first digit occurs in those four letters, we have to check for all possible positions. For this I would do away with the lookaheads altogether, and simply provide the three different alternatives. (I will keep the lookbehind as an optimization for non-matching parts.)
'/(?<![A-Z])[A-Z]\s*(?:\d\s*[A-Z]\s*[A-Z]|[A-Z]\s*\d\s*[A-Z]|[A-Z]\s*[A-Z][A-Z\s]*\d?)[A-Z\s]*[A-Z]/'
Or here with added comments:
'/
(?<! # negative lookbehind
[A-Z] # current position is not preceded by a letter
) # end of lookbehind
[A-Z] # match has to start with uppercase letter
\s* # optional spaces after first letter
(?: # subpattern for possible digit positions
\d\s*[A-Z]\s*[A-Z]
# digit comes after first letter, we need two more letters before last one
| # OR
[A-Z]\s*\d\s*[A-Z]
# digit comes after second letter, we need one more letter before last one
| # OR
[A-Z]\s*[A-Z][A-Z\s]*\d?
# digit comes after third letter, or later, or not at all
) # end of subpattern for possible digit positions
[A-Z\s]* # arbitrary amount of further letters and whitespace
[A-Z] # match has to end with uppercase letter
/x'
That gives the same result on Ωmega's lengthy test input.
I suggest to use regex pattern
[A-Z][ ]*(\d)?(?(1)(?:[ ]*[A-Z]){3,}|[A-Z][ ]*(\d)?(?(2)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(\d)?(?(3)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(?:\d|(?:[ ]*[A-Z])+[ ]*\d?))))(?:[ ]*[A-Z])*
(see this demo).
[A-Z][ ]*(?:\d(?:[ ]*[A-Z]){2}|[A-Z][ ]*\d[ ]*[A-Z]|(?:[A-Z][ ]*){2,}\d?)[A-Z ]*[A-Z]
(see this demo)
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.