0

I would like to match a portion of a URL in this order.

First the domain name will remain static. So, nothing check with regex.

$domain_name = "http://foo.com/"; 

What I would like to validate is what comes after the last /.

So, my AIM is to create something like.

$stings_only            = "[\w+]";
$number_only            = "[\d+]";
$numbers_and_strings    = "[0-9][a-z][A-Z]"; 

Now, I would like to just use the above variables to check if a URL confirms to the patterns mentioned.

$example_url = "http://foo.com/some-title-with-id-1"; 

var_dump(preg_match({$domain_name}{$strings_only}, $example_url)); 

The above should return false, because title is NOT $string_only.

$example_url = "http://foo.com/foobartar"; 

var_dump(preg_match({$domain_name}{$strings_only}, $example_url)); 

The above should return true, because title is $string_only.

4
  • 2
    1. You really need to read about using regex 2. You don't need regex for this problem. Commented May 14, 2014 at 21:25
  • @anubhava You don't understand. The /./ is just an example. It could also be \d+ or any other pattern. So, regex is needed Commented May 14, 2014 at 21:55
  • 1
    regex is needed is a very bold statement. Commented May 14, 2014 at 21:58
  • Nice read. Thanks for the share Commented May 14, 2014 at 22:09

1 Answer 1

2

Update:

~^http://foo\.com/[a-z]+/?$~i
~^http://foo\.com/[0-9]+/?$~
~^http://foo\.com/[a-z0-9]+/?$~i

These would be your three expressions to match alphabetical URLs, numeric URLS, and alphanumeric. A couple notes, \w matches [a-zA-Z0-9_] so I don't think it is what you expected. The + inside of your character class ([]) does not have any special meaning, like you may expect. \w and \d are "shorthand character classes" and do not need to be within the [] syntax (however they can be, e.g. [\w.,]). Notice the i modifier, this makes the expressions case-insensitive so we do not need to use [a-zA-Z].

$strings_only = '~^http://foo\.com/[a-z]+/?$~i';

$url = 'http://foo.com/some-title-with-id-1';
var_dump(preg_match($strings_only, $url)); // int(0)

$url = 'http://foo.com/foobartar';
var_dump(preg_match($strings_only, $url)); // int(1)

Test/tweak all of my above expressions with Regex101.


. matches any character, but only once. Use .* for 0+ or .+ for 1+. However, these will be greedy and match your whole string and can potentially cause problems. You can make it lazy by adding ? to the end of them (meaning it will stop as soon as it sees the next character /). Or, you can specify anything but a / using a negative character class [^/].

My final regex of choice would be:

~^https://stolak\.ru/([^/]+)/?$~

Notice the ~ delimiters, so that you don't need to escape every /. Also, you need to escape the . with \ since it has a special meaning. I threw the [^/]+ URI parameter into a capture group and made the trailing slash optional by using /?. Finally, I anchored this to the beginning and the end of the strings (^ and $, respectively).

Your question was somewhat vague, so I tried to interpret what you wanted to match. If I was wrong, let me know and I can update it. However, I tried to explain it all so that you could learn and tweak it to your needs. Also, play with my Regex101 link -- it will make testing easier.


Implementation:

$pattern = '~^https://stolak\.ru/([^/]+)/?$~';
$url = 'https://stolak.ru/car-type-b1';

preg_match($pattern, $url, $matches);
var_dump($matches);

// array(2) {
//   [0]=>
//   string(29) "https://stolak.ru/car-type-b1"
//   [1]=>
//   string(11) "car-type-b1"
// }
Sign up to request clarification or add additional context in comments.

4 Comments

getting error Warning: preg_match(): No ending delimiter '~' found
You copied the expression wrong, make sure you have the ~ on both sides...see my PHP code in the edit.
Still this did not answer the problem. Thanks for the effort though.
Did you even read through my answer? Or just try to plug it in.. RE: "Your question was somewhat vague, so I tried to interpret what you wanted to match. If I was wrong, let me know and I can update it." -- update your question and I can help, no one else will be able to do anything better than a guess.

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.