Text: https://vk.com/id1

It is necessary to select in the text id 'unit'

Text: https://vk.com/id33

It will be 33

Closed due to the fact that the essence of the issue is unclear by the participants user194374, pavel , Denis , tutankhamun , Grundy 9 Dec '16 at 9:16

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    100 rubles :) /vk\.com\/id(\d+)/ - ilyaplot

3 answers 3

You can use online constructors for writing regular expressions, for example http://uvsoftium.ru/php/regexp.php

There is a detailed reference on writing.

In your case, it looks like: vk\.com\/id(\d+)

    Something like this:

     /https?:\/\/vk\.com\/id(-?\d+)/ 

    And maybe so:

     /https?:\/\/vk\.com\/(?:id)?(-?\w+)/ 
    • one
      Negative numbers can not be. - andreymal
    • @andreymal, groups have negative id. - Qwertiy
    • Group addresses begin with club, public and event, but not with id. And the minus still does not contain, by the way - andreymal
    • @andreymal, hmm .. looked. Minus appears in the audio and video lists of groups. - Qwertiy
    • Here it happens, yes. And in the photos and on the wall. But not with id) - andreymal

    Example in php:

     $vk_url = "https://vk.com/id33"; $string = preg_replace("/https?:\/\/vk\.com\/id/", '', $vk_url); 

    As an example without a request to the VK API, but it is better to check through the VK API to remove cases when screen_name starts with id

    But now screen_name is very often used and it is necessary to check it with id or screen_name and already considering this, build a request to the API and check and write down either id or numbers:

     $vk_url = "https://vk.com/id33"; $string = preg_replace("/https?:\/\/vk\.com\/(id:?)?/", '', $vk_url);