php - Regexp - How to check whether a string starts OR ends with given character? -


string start , ends character should not match.

is easiest like:

$string = '!test'; preg_match('/^(!?)([a-z0-9]{1,10})(!?)$/i', $string, $matches); 

and examine $matches?

if have 1 character @ beginning or end, it's simple saying this:

/^!|!$/ 

no need capture group.


update: make sure either an exclamation mark , ten letters or ten letters , exclamation mark, build pattern this:

/^!?[a-z0-9]{1,10}$|^[a-z0-9]{1,10}!?$/ 

it looks it's repeating itself, it's way cheaper , faster having 3 capture groups.

i put a unit test in regex101 this.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -