php - regular expression - match
Get the solution ↓↓↓Solution:
One of the most difficult things about using regexes is resisting the lure of excessive cleverness. You've got two separate, incompatible tests to perform. Length:
/^.{2,24}$/
...and composition:
/^[A-Z0-9]+(?:-[A-Z0-9]+)*$/i
While it might be possible to come up with a regex that will perform both tests in one pass, there's no need. Just do one of them as a lookahead:
/^(?=.{2,24}$)[A-Z0-9]+(?:-[A-Z0-9]+)*$/i
Answer
Solution:
Try this PCRE reg exp:
/^(([0-9a-z]+-)*[0-9a-z]){2,24}$/i
Answer
Solution:
Regular expressions depend on the platform you are using, and they are not very good for matching sizes (if you try to determine a specific length, you will probably loose flexibility, unless you are checking more standardized inputs, like dates:\d{2}/\d{2}/\d{4})
.
But something like:
[a-z]+(-[a-z]+)*
may give you an idea for your problem.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: cannot access offset of type string on string
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.