Page 1 of 1

Help with RegEx

Posted: Wed Oct 26, 2016 3:47 pm
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?

Re: Help with RegEx

Posted: Wed Oct 26, 2016 4:26 pm
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.

Re: Help with RegEx

Posted: Thu Oct 27, 2016 9:11 am
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

Re: Help with RegEx

Posted: Wed Nov 02, 2016 2:30 pm
by loicaigon
Hi Terkelsen

This regexp seems to do it:

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

HTH

Loic
www.ozalto.com

Re: Help with RegEx

Posted: Thu Nov 03, 2016 11:40 am
by Terkelsen
Hi Loic,

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

Thanks a lot.

Re: Help with RegEx

Posted: Thu Nov 03, 2016 12:12 pm
by loicaigon
You're welcome !