Forum Moderators: coopster & phranque

Message Too Old, No Replies

RegExp issue

can't get an optional to work

         

mattglet

8:07 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the pattern:
"(^¦[ \t\r\n])((ftp¦http¦https¦gopher¦mailto¦news¦nntp¦telnet¦wais¦file¦prospero¦aim¦webcal): (([A-Za-z0-9$_.+!*(),;/?:@&~=-])¦%[A-Fa-f0-9]{2})+(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?)"

I can't seem to get the protocol to be optional. I'm trying to get both "http://www.example.com" and "www.example.com" to match. Thanks for any help given.

[edited by: jatar_k at 8:26 pm (utc) on Feb. 5, 2005]
[edit reason] turned off smilies [/edit]

coopster

1:54 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi mattglet, I pulled a bit of your other information from the other post you started...


I can't seem to get the protocol to be optional. I'm trying to get both "http://www.example.com" and "www.example.com" to match (as it stands, a match only occurs with http: //, etc. present). I can't get the? in the correct spot, without messing the whole thing up. Optimally, I would like the colon and slashes to be optional too (i.e. http: //).Thanks for any help given.

Have you considered rolling the '://' into your optionals? For example,

(ftp://¦http://¦https://¦gopher://) etc.

Could you explain a bit more what you are trying to accomplish?

mattglet

2:19 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm making some forum functionality that will auto-activate any URLs entered in text (like it does here). For example, if you have:
"This is a test sentence, check out my site at [example.com",...] I want to have the URL linked when it displays after post. Make sense?

Your suggestion might not work, b/c as it stands, the http :// is not optional in that pattern. I need to make it optional.

mattglet

4:38 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I stepped away from it, and put in some more work. I ended up simplifying it a little, and found a solution.

((^¦[ \t\r\n])(((http¦https¦ftp)\://)?)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s])

This pattern will match any url, with the protocol being optional. Combine it with some functionality that adds a link, and you've got an auto-activating url.