RegEx just melts my brain :(

LOL… correct again… guess I need to find a better pattern… or if it fails, try again with “HTTP://” prepended

Well the code doesn’t work with capital letters, so you must try with “http://” :smiley:

I just took RegExRX and copied Kem’s Search pattern into it. then i enclosed the Search in () to get a Subexpression and created a Replace Pattern. Then i let RegExRX create native Xojo Code (MBS Code is also supported if needed).

The result is:

[code]dim rx as new RegEx
rx.SearchPattern = _
“(?xim-Us) # FREE SPACING, case-insensitive, greedy” + EndOfLine + _
“” + EndOfLine + _
“# Define the prefix” + EndOfLine + _
“(?(DEFINE)(?[A-Z]{3,}://))” + EndOfLine + _
“# Define a valid URL character” + EndOfLine + _
“(?(DEFINE)(?[A-Z0-9\-_~:/?\#[\]@!$&’()*+;=.,%]))” + EndOfLine + _
“” + EndOfLine + _
“# START” + EndOfLine + _
“(\b # Word boundary” + EndOfLine + _
“(?: # Non-capturing group” + EndOfLine + _
“(?<=\<)(?&prefix)(?&valid)+(?=\>) # Anything between angle-brackets” + EndOfLine + _
“| # OR” + EndOfLine + _
“(?<=\[)(?&prefix)(?&valid)+(?=\]) # Anything between square-brackets” + EndOfLine + _
“| # OR” + EndOfLine + _
“(?<=\{)(?&prefix)(?&valid)+(?=\}) # Anything between curly-brackets” + EndOfLine + _
“| # OR” + EndOfLine + _
“(?&prefix)(?&valid)+(?<![\.,]) # Can’t end on a dot or comma” + EndOfLine + _
“) )# End non-capturing group”
rx.ReplacementPattern = “my favorite webiste is $3"”"

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
rxOptions.ReplaceAllMatches = true

dim replacedText as string = rx.Replace( sourceText )
[/code]

Or without RegEx Comments:

[code]dim rx as new RegEx
rx.SearchPattern = _
“(?xim-Us)(?(DEFINE)(?[A-Z]{3,}://))(?(DEFINE)(?[A-Z0-9\-_~:/?\#[\]@!$&’()*+;=.,%]))(\b(?:(?<=\<)(?&prefix” + _
“)(?&valid)+(?=\>)|(?<=\[)(?&prefix)(?&valid)(?=\])|(?<=\{)(?&prefix)(?&valid)+(?=\})|(?&prefix)(?&valid)+(?<![\.,])) )”
rx.ReplacementPattern = “my favorite webiste is $3"”"

dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
rxOptions.ReplaceAllMatches = true

dim replacedText as string = rx.Replace( sourceText )
[/code]

Very interesting the information on regex101.com You can put the code and they show the explanation for each part.

For people that learn by example could be useful.

[quote=400021:@Alberto De Poo]Very interesting the information on regex101.com You can put the code and they show the explanation for each part.

For people that learn by example could be useful.[/quote]

Funny. Today i’ve written an Email to them asking for Xojo Support in the Code Generator. :wink:

Sascha… that “kinda” fits my criteria, however

  • link must start with HTTP or that pattern fails
  • processes URLS within () a criteria I specified had to be ignored

but otherwise :slight_smile:

I have a working solution, see above…
it does require the URL to be tested start with HTTP(s):// or WWW.
if WWW it adds HTTP:// (if it were supposed to be HTTPS:// then that falls on the user)

Private Function isValidURL(url as string) as boolean
  Dim r As New RegEx
  // does it start with HTTP:// or HTTPS://  or WWW.
  If Left(URL,4)="WWW." Then url="HTTP://"+url
  If Left(url,7)="HTTP://" Or Left(url,8)="HTTPS://" Then  
    r.SearchPattern ="^(?:(?:https?|ftp):\\/\\/)(?:\\S+(?::\\S*)?@)?(?:(?!10(?:\\.\\d{1,3}){3})(?!127(?:\\.\\d{1,3}){3})(?!169\\.254(?:\\.\\d{1,3}){2})(?!192\\.168(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z0-9]+-?)*[a-z0-9]+)(?:\\.(?:[a-z0-9]+-?)*[a-z0-9]+)*(?:\\.(?:[a-z]{2,})))(?::\\d{2,5})?(?:/[^\\s]*)?$"
    Return (r.Search(url.Lowercase) <> Nil)
  Else
    Return False
  End If
End Function

If you allow, i’d recommend to add a HTTPS instead, because rather sooner than later https will be a “standard”.

the problem (at least for now) with that is, that if it is TRULY an HTTP:// site, you get an error if you use HTTPS
but (at least for now) most HTTPS: sites (like google) do a redirect if you use HTTP

[quote=399973:@Sascha S]Link to RegExRX in the Mac App Store

It’s really worth every cent! :)[/quote]

Yes, but hope the developer build and update the App in the Store because it’s displaying the warning about it’s not optimized and must update (the 32 bit thing and future macOS releases) https://support.apple.com/en-us/HT208436

I am sure, Kem will do when/if needed. :slight_smile:

Yeah, yeah… :slight_smile:

I rely on it too, so it will be updated before it makes a difference.