RegEx just melts my brain :(

  1. ‹ Older
  2. 3 months ago

    Alberto D

    Aug 9 Pre-Release Testers, Xojo Pro
    Edited 3 months ago

    Yes, my point is not if it's possible, my point is that it is easy to have.it.wrong

    See what the forum did

    Sorry that I can't help you to even do what this forum does. Maybe some day I will learn the basics.

  3. Dave S

    Aug 9 San Diego, California USA
    Edited 3 months ago

    This seems to work... no freaking clue how or why (found elsewhere on this forum)

    Function IsValidURL(url As String) As Boolean
      Dim r As New RegEx
      
      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) <> Nil)
      
    End Function

    however I have to split out the strings to test...... which is fine

    and Alberto.... why is the link in your example "wrong"... it might not be a real URL, but techinally it is valid

  4. Dave S

    Aug 9 Answer San Diego, California USA

    Thank you all... I found a method that works, and is 'fast enough'

    • Given a string that "might" contain a URL
    • split the string on "space" boundaries [assumption... URL has %20 instead of ' ']
    • check each sub string against above RegEx
    • if TRUE, using Instr(original,substring), replace it with new pattern
  5. Dave S

    Aug 9 San Diego, California USA

    @Alberto D;Poo Just FYI
    https://regex101.com/ there is a pattern error on the one you posted also doing some tests there (after fixing the pattern) it doesn't match 'google.com' but it does match 'http://google.com'

    Thanks, and you are correct... however, in this app, its up to the user to take some responsibily...
    So if they type in google.com and the app does not make a link out it, then they just need to make it "more valid" :)

  6. Dave S

    Aug 9 San Diego, California USA

    this is what I would liked to achieve, but what I have is good enough for this application

    http://soapbox.github.io/linkifyjs/

  7. Alberto D

    Aug 9 Pre-Release Testers, Xojo Pro

    @Dave S and Alberto.... why is the link in your example "wrong"... it might not be a real URL, but techinally it is valid

    Because the forum see .it and then creates a link but not with other characters:
    have.it.wrong
    have.pp.wrong

    either the URL with it pp should also be linked or the process to create link should check if it is the last word and only then link that. If the first one is valid URL (but wrong), then the second should be valid too.

    Anyway, that doesn't matter, it was just an observation on how the forum do things.

  8. Alberto D

    Aug 9 Pre-Release Testers, Xojo Pro

    @Dave S Thanks, and you are correct... however, in this app, its up to the user to take some responsibily...
    So if they type in google.com and the app does not make a link out it, then they just need to make it "more valid" :)

    But you said:

    @Dave S I need a function that does the following

    s="my favorite website is www.rdsisemore.com"
    s=FixTheURL(s)

    would become
    my favorite webiste is [](http://www.rdsisemore.com)"

    EDIT : actually.... it would need to become this
    my favorite webiste is [http://www.rdsisemore.com](http://www.rdsisemore.com)"

    But it's ok, is your program and you know what you want. I'm glad that you got the solution.

  9. Dave S

    Aug 9 San Diego, California USA

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

  10. Alberto D

    Aug 9 Pre-Release Testers, Xojo Pro

    Well the code doesn't work with capital letters, so you must try with "http://" :D

  11. Sascha S

    Aug 9 Pre-Release Testers, Xojo Pro Germany/W'haven
    Edited 3 months ago

    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:

    dim rx as new RegEx
    rx.SearchPattern = _
    "(?xim-Us) # FREE SPACING, case-insensitive, greedy" + EndOfLine + _
    "" + EndOfLine + _
    "# Define the prefix" + EndOfLine + _
    "(?(DEFINE)(?<prefix>[A-Z]{3,}://))" + EndOfLine + _
    "# Define a valid URL character" + EndOfLine + _
    "(?(DEFINE)(?<valid>[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]($3)"""
    
    dim rxOptions as RegExOptions = rx.Options
    rxOptions.LineEndType = 4
    rxOptions.ReplaceAllMatches = true
    
    dim replacedText as string = rx.Replace( sourceText )

    Or without RegEx Comments:

    dim rx as new RegEx
    rx.SearchPattern = _
    "(?xim-Us)(?(DEFINE)(?<prefix>[A-Z]{3,}://))(?(DEFINE)(?<valid>[A-Z0-9\-_~:/?\#[\]@!$&'()*+;=.,%]))(\b(?:(?<=\<)(?&prefix" + _
    ")(?&valid)+(?=\>)|(?<=\[)(?&prefix)(?&valid)(?=\])|(?<=\{)(?&prefix)(?&valid)+(?=\})|(?&prefix)(?&valid)+(?<![\.,])) )"
    rx.ReplacementPattern = "my favorite webiste is [$3]($3)"""
    
    dim rxOptions as RegExOptions = rx.Options
    rxOptions.LineEndType = 4
    rxOptions.ReplaceAllMatches = true
    
    dim replacedText as string = rx.Replace( sourceText )
  12. Alberto D

    Aug 9 Pre-Release Testers, Xojo Pro

    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.

  13. Sascha S

    Aug 9 Pre-Release Testers, Xojo Pro Germany/W'haven

    @Alberto D;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.

    Funny. Today i've written an Email to them asking for Xojo Support in the Code Generator. ;)

  14. Dave S

    Aug 9 San Diego, California USA

    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 :)

    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
  15. Sascha S

    Aug 9 Pre-Release Testers, Xojo Pro Germany/W'haven

    @Dave S if WWW it adds HTTP:// (if it were supposed to be HTTPS:// then that falls on the user)

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

  16. Dave S

    Aug 9 San Diego, California USA

    @SaschaSchneppmueller 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

  17. José M

    Aug 9 Pre-Release Testers, Xojo Pro Spain

    @SaschaSchneppmueller Link to RegExRX in the Mac App Store

    It's really worth every cent! :)

    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

  18. Sascha S

    Aug 9 Pre-Release Testers, Xojo Pro Germany/W'haven

    @José M&iacute;a Terry Jiménez 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. :)

  19. Kem T

    Aug 9 Pre-Release Testers, Xojo Pro, XDC Speakers New York

    Yeah, yeah... :)

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

or Sign Up to reply!