0

I am trying to verify if a string is properly formatted. Below are a few of the formats that come through. These are just random typed items.

1abc.abc

ab2.abc

abc.abc.abt

aerj/adfa.tht

The before the . can be symbols alpha and numeric. After the . The check im trying to do is check the end of the string for the . followed by no more than 4 alpha characters. I believe this can be done with regex but I do not know where to start on it.

An invalid string is anything containing numbers, symbols or greater than 4 characters following the period

5
  • Please look up regular expressions. Commented Oct 12, 2012 at 20:22
  • and or give a good example of a valid string and an invalid string Commented Oct 12, 2012 at 20:23
  • an invalid string is anything with symbols, numbers or longer than 4 characters following the . Commented Oct 12, 2012 at 20:24
  • Would abc. be valid? How about .? or .abc? Commented Oct 12, 2012 at 20:26
  • There must be something before and after the period Commented Oct 12, 2012 at 20:37

2 Answers 2

3

Try this (match all alphanumeric/symbols, but no spaces):

^([\S]+)\.([A-Za-z]{0,4})$

Same, but with spaces matched:

^(.*?)+\.([A-Za-z]{0,4})$

More specifically:

if (preg_match("/^([\S]+)\.([A-Za-z]{0,4})$/", $yourStr) == 1) { }
Sign up to request clarification or add additional context in comments.

6 Comments

. matches anything, not just alphanumeric.
He said symbols, alpha, and number. I guess it would match spaces too, though...
I'm an idiot and completely read over symbols by accident. Although yeah, matches spaces ;)
In which case, use \w (or \W, whichever is not whitespace).
There is still one issue. In the instance of "abc.abc.abt" it fails
|
1

You should go with regex pattern /\.[a-zA-Z]{0,4}$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.