AlecSadler

Using regex to make a link clickable

9 posts in this topic

#1 ·  Posted

Hello friends! I am rather new to using regular expressions. So please mind my noob question.

 

I have an instant messenger and I use in it an embeded IE control to display messages.

 

How would I go about detecting links (http and https) and turn them into anchors <a href="https://someurl.org/blahblah">https://someurl.org/blahblah

 

Thanks much in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted

Have you tried StringRegExpReplace?


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#3 ·  Posted

3 minutes ago, TheDcoder said:

Have you tried StringRegExpReplace?

I don't know how I would go about doing this, I need to take an existing string and put it into an anchor link. Could you please show an example?

Share this post


Link to post
Share on other sites

#4 ·  Posted

You can make a RegEx (or find it from the internet) for detecting the URLs and append the detected urls with the HTML tags... If you actually do not know how RegEx works, I suggest you to wait for someone else with a magical RegEx pattern to post here, we have a lot of RegEx wizards in the forums ;)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

This raw example assumes that the links in the text are ending with white spaces. If it is not the case then the pattern must be adapted in the regular expression

Local $text = "here is a link https://someurl.org/blahblah in the text, " & @crlf & _ 
        "and here is http://otherurl.org/blah another one"
        
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

 

Edited by mikell

Share this post


Link to post
Share on other sites

#7 ·  Posted

16 hours ago, mikell said:

This raw example assumes that the links in the text are ending with white spaces. If it is not the case then the pattern must be adapted in the regular expression

Local $text = "here is a link https://someurl.org/blahblah in the text, " & @crlf & _ 
        "and here is http://otherurl.org/blah another one"
        
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

 

Thanks, this works perfect for links that are followed by a white-space, but what about raw links sent without any white spaces behind them? How to go about detecting a link when not followed by a space? (how do I detect the end of the link when there's nothing following it)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Did you try my code in this case ?

Local $text = "here is a link https://someurl.org/blahblah"     
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

If it doesn't work then please provide all the precise requirements - may links be enclosed by other characters and which ones
The best would be to post a txt with some examples of the string(s) you have to treat

Edited by mikell

Share this post


Link to post
Share on other sites

#9 ·  Posted

On 7/16/2017 at 1:40 AM, mikell said:

Did you try my code in this case ?

Local $text = "here is a link https://someurl.org/blahblah"     
$text = StringRegExpReplace($text, '(https?\S+)', '<a href="$1">$1</a>')
Msgbox(0,"", $text)

If it doesn't work then please provide all the precise requirements - may links be enclosed by other characters and which ones
The best would be to post a txt with some examples of the string(s) you have to treat

Okay this code is working as I need it to, no changes needed!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now