Making regex work...

Post

Posted
Rating:
#1 (In Topic #1374)
Regular
chrisRoald is in the usergroup ‘Regular’
Hi, does anyone know how to get this Regex string to work in gambas interpreter?   :oops:
( string is a suggested URL match! ?)

Image

(Click to enlarge)


Thanks,
     C.
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
It is because regex escape characters are different to gambas.

you have to use 2 backslashes \ for each regex specific escape.

2 backslashes \ in gambas is \   so for example \/ that you may use in regex but not gambas has to be input as \/

Also all the other chars like \w will have to be \w ,  \$ \+ and so on.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
chrisRoald is in the usergroup ‘Regular’
Thanks Bruce,
    Interpreter is happy with the expression string now all '\' are '\'  :)
But am getting this error in the Regexp.Match line of code:-

Image

(Click to enlarge)


Will try other suggested (online) web URL-match expressions, but any from the forum gratefully received.

Thanks,
    C.
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
 Sorry Chris I cannot help further , i know very little about complex regex expressions.

I'd use something simpler like Like

If webURL Like "{http,https,ftp}://*" Then

Although I don't really know what exactly you are checking for.
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
chrisRoald is in the usergroup ‘Regular’
Hi, re. the above issues, I just thought this might be useful if you're developing a straight forward Webview …

I needed a view and URL textbox that the user could enter or paste the URL of a webpage/download. Also wanted the input value to be compliant with URI syntax before the web request is submitted to the webView.url property.

After a few attempts, along with Bruce's help with string escape characters, I came up with the following code:-
The Regex match-string used here was the most reliable - and error-free - that I could find online. (scoured stackoverflow.com,
but there are many other sites with ideas for 'best' URI Regex - note: some raise errors, so maybe use Try command!).

Code (gambas)

  1. Public Sub metaWebURL_Activate()
  2.  
  3.     Dim webUrl As String
  4.     Dim regexA As String
  5.    
  6.     webUrl = metaWebURL.text
  7.     regexA =
  8. "(([\\w]+:)?//)?(([\\d\\w]|%[a-fA-f\\d]\{2,2})+(:([\\d\\w]|%[a-fA-f\\d]\{2,2})+)?@)?([\\d\\w][-\\d\\w]\{0,253}[\\d\\w]\\.)+[\\w]\{2,63}(:[\\d]+)?(/([-+_~.\\d\\w]|%[a-fA-f\\d]\{2,2})*)*(\\?(&?([-+_~.\\d\\w]|%[a-fA-f\\d]\{2,2})=?)*)?(#([-+_~.\\d\\w]|%[a-fA-f\\d]\{2,2})*)?"
  9.  
  10.     If RegExp.Match(webUrl, regexA) Then
  11.         metaWebURL.text = webUrl & " is downloading, please wait..."
  12.         WebView1.Url = webUrl
  13.     Else
  14.         metaWebURL.text = webUrl & " is not URI compliant"
  15.    Endif
  16.    

Hope this helpful to folk.
C.
Online now: No Back to the top
1 guest and 0 members have just viewed this.