Auto-linking Reddit References

Looking for an Extension? Have an Extension request? Post your request here for help. (Note: This forum is community supported; while there is an Extensions Development Team, said team does not dedicate itself to handling requests in this forum)
Ideas Centre
User avatar
JoshyPHP
Code Contributor
Posts: 1012
Joined: Mon Jul 11, 2011 12:28 am

Re: Auto-linking Reddit References

Post by JoshyPHP » Wed Dec 12, 2018 1:00 pm

AbaddonOrmuz wrote:
Wed Dec 12, 2018 6:19 am
It checks that the username and subreddit does not start with _
More precisely, #\br/([^_]\w+)\b# checks that it starts with anything except _. That includes whitespace and other things you wouldn't want in the username. Since this is just a test, it's better to use a lookahead assertion, i.e. #\br/(?!_)(\w+)\b#

Also, if I read correctly, usernames can use dashes so the regexp should probably be different. And you should definitely give names to the captures otherwise default ones will be chosen for you behind the scenes. The PHP line should be something like that:

Code: Select all

'#\\bu/(?![-_])(?<username>[-\\w]+)\\b#',
I wrote the thing that does BBCodes in 3.2.

User avatar
AbaddonOrmuz
Registered User
Posts: 659
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo Ramos
Contact:

Re: Auto-linking Reddit References

Post by AbaddonOrmuz » Wed Dec 12, 2018 6:33 pm

JoshyPHP wrote:
Wed Dec 12, 2018 1:00 pm
It checks that the username and subreddit does not start with _

More precisely, #\br/([^_]\w+)\b# checks that it starts with anything except _. That includes whitespace and other things you wouldn't want in the username. Since this is just a test, it's better to use a lookahead assertion, i.e. #\br/(?!_)(\w+)\b#

Also, if I read correctly, usernames can use dashes so the regexp should probably be different. And you should definitely give names to the captures otherwise default ones will be chosen for you behind the scenes. The PHP line should be something like that:

Code: Select all

'#\\bu/(?![-_])(?<username>[-\\w]+)\\b#',
Right, I didn't test it with whitespaces, that would break things.

Trying to improve the regex even further, it should be limited to a range of 3-20 characters (Reddit's limit), otherwise it could match very long words.

Code: Select all

#\\bu/(?![_-])(?<user>[\\w-]{3,20})\\b#
Some of my phpBB extensions: [ Imgur | SEO Metadata | Markdown ]
Check out all my extensions
Arch Linux user

Post Reply

Return to “Extension Requests”