Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: select only urls from an unusual string

In my old database there are fields of such content:

a:2:{s:6:"item-0";a:1:{s:17:"siteurl";s:25:"https://www.urlsite.com/";}s:6:"item-1";a:1:{s:17:"siteurl";s:29:"https://urlsite2.org/";};a:1:{s:17:"siteurl";s:29:"https://urlsite3.com/";}}

I need to select only the url from this, so that something like this happens:

https://www.urlsite.com/,https://urlsite2.org/,https://urlsite3.com/

At the moment I use this construction:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.*?(http.*?(\.org|\.com))

However, I get this result:

https://www.urlsite.com,https://urlsite2.org,https://urlsite3.com,/";}}

It is not possible to select the end of the line after the last url and it gets into the result.

,/";}}

>Solution :

A JavaScript implementation:

const text =
  'a:2:{s:6:"item-0";a:1:{s:17:"siteurl";s:25:"https://www.urlsite.com/";}s:6:"item-1";a:1:{s:17:"siteurl";s:29:"https://urlsite2.org/";};a:1:{s:17:"siteurl";s:29:"https://urlsite3.com/";}}';

const result = text.match(/http.*?(.com|.org)\//gm);

console.log(result.join(','));

Results in:

https://www.urlsite.com/,https://urlsite2.org/,https://urlsite3.com/
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading