Help with RegEx

Post Reply
User avatar
Terkelsen
Advanced member
Posts: 336
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Help with RegEx

Post by Terkelsen »

I have found the following Regular Expression very useful to look for a URL in a long text string:

(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)

Used on the following text string:

<td bgcolor="white" style="padding:5px !important;” class=“”><a href="https://www.jotformeu.com/uploads/Terke ... /DEMO.xlsx" class="">DEMO.xlsx</a></td>

... it will return this:

https://www.jotformeu.com/uploads/Terke ... /DEMO.xlsx"

It works very well with variables in Switch. The only problem is the dobbelt quote at the end of the string, so I changed the RegEx to this:

(ftp:\/\/|www\.|https?:\/\/){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}\.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S[^"]*)

... adding [^"] to Group 2

Checking this in different online RegEx evaluators, it works fine, but if I use it in Switch, it breaks everything and only returns this: *)"].

Could anybody tell me how to change the RegEx to exclude the dobbelt quote at the end of the URL, AND have it work in Switch?
User avatar
TheCaptain
Member
Posts: 86
Joined: Mon Jan 18, 2016 4:22 pm
Location: London

Re: Help with RegEx

Post by TheCaptain »

I've found Switch likes having this expression

Code: Select all

{2,}
fully quantified i.e

Code: Select all

{2,6}
... and often works best without the comma if that's possible.

Also, where I've had a longer expression, sometimes enclosing the entire thing once more in '(' and ')' can help.

from

Code: Select all

(this{2})\b
to this

Code: Select all

((this{2})\b)
sort of thing.
User avatar
Terkelsen
Advanced member
Posts: 336
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Help with RegEx

Post by Terkelsen »

Excuse my ignorance, but I don't think I understand how this could solve the problem?

I need a way to exclude the double quote (") from the result that is returned. In a javascript RegEx this would work

Code: Select all

[^"] 
but it can't be used inside Switch
loicaigon
Advanced member
Posts: 633
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with RegEx

Post by loicaigon »

Hi Terkelsen

This regexp seems to do it:

(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?

HTH

Loic
www.ozalto.com
Loïc Aigon
Enfocus PitStop Manager
User avatar
Terkelsen
Advanced member
Posts: 336
Joined: Thu Sep 08, 2011 5:08 pm
Contact:

Re: Help with RegEx

Post by Terkelsen »

Hi Loic,

That was a different approach but it solves the problem ;)

Thanks a lot.
loicaigon
Advanced member
Posts: 633
Joined: Wed Jul 10, 2013 10:22 am

Re: Help with RegEx

Post by loicaigon »

You're welcome !
Loïc Aigon
Enfocus PitStop Manager
Post Reply