A
A
AleDv2016-05-13 20:33:39
PHP
AleDv, 2016-05-13 20:33:39

How to use regular expressions to catch links in Vkontakte?

Hello, friends. There is a text of the post in Vkontakte:


sample text, sample text, sample text, sample text. [id123456|User].
More text, sample text, sample text, sample text, sample text [club987654321|Community] , sample text, sample text, sample text.

I need to isolate two links from this text:
id123456|User,
club987654321|Community.
With the help of the code preg_match_all('/\[(.+)\]/',$text,$found);, it was not possible to get the desired.
I ask for help: how to redo the regular expression to get all the links from the post. I must have made a stupid mistake somewhere.
Thank you for your help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zelimkhan Beltoev, 2016-05-13
@Beltoev

In general, the regular expression works: it extracts what you need ( https://regex101.com/r/yS7wI4/1 )
I suspect that you are using the preg_match_all function in a wrong way .
Such a regular expression can search even more accurately: \[((?:id|club)\d+)[|](.*)\]
( https://regex101.com/r/gS7xQ0/1 )
New regular expression:\[((?:id|club)\d+)[|]([^]]*)\]

A
Alexander N++, 2016-05-13
@sanchezzzhak

#id([0-9]+)#i
#club([0-9]+)#i

B
bashkarev, 2016-05-14
@bashkarev

/(?<type>id|club)(?<id>\d+)\|(?<name>\W+)/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question