S
S
Scuba2018-08-11 15:32:17
PHP
Scuba, 2018-08-11 15:32:17

How to correctly compose a regular expression pattern?

In general, you need to check the url address for the VKontakte profile.
There can be (or not be) one dot, as many underscores, English letters and numbers as you like, the length of the string is from 2 to 33 characters inclusive, not counting the beginning of https://vk.com/.
I've done like this, but it doesn't work, in general.
For example, the string " https://vk.com/abc... " is considered valid.

preg_match('#^https://vk.com/[\d\D_(\.?)]{2,33}$#i', $_POST['source'])

And I tried it like this:
preg_match('#^https://vk.com/[(\d)*(\D)*(\.)?(_)*]{2,33}$#i', $_POST['source'])

How to make such a check? Ideally, it would also be added to the characters . and _ could not be at the beginning of the pattern and at the end, and that they should not stand next to each other like this: abc__abc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2018-08-11
@targetologru

For example, like this:
And it's better to check for length using strlen ()

A
ant2015, 2018-08-11
@ant2015

The idea is so
The first and last character can simply be checked with the if-else branch statement and the standard substr function. At the same time, check for invalid words in a string (club for communities, etc.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question