SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Regexp question

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regexp question

    Hello,

    I have enabled #hashtags on my forum... here's the replacement code:

    Code php:
    $string = preg_replace('/(^|\s)#(\w*[a-zA-Z0-9_]+\w*)/', '\1<a href="http://mysite.com/link_to_hash/\2">#\2</a>', $string)
    How can I make it so hashtags CANNOT begin with a NUMBER (0-9), but can begin with a-zA-Z_?

    Thanks!

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Noone's jumped on this for a couple hours, so i'll put me foot on the grenade and try and teach at the same time.

    Define your hashtag.

    I'll give you an example, that i'm going to lift from my old college's homework.
    A valid string is one such that there is at least 1 a, followed by a b, followed by any number of b's and c's.
    /a+b[bc]*/
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,084
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I'd use this

    ~(^|\s)#([a-zA-Z][a-zA-Z0-9_]*)~

    ~ - start regular expression
    ( - start capture group 1
    ^|\s - match at either the start of the string, or after a space
    ) - end capture group 1
    # - match a # literally
    ( - start capture group 2
    [a-zA-Z] - match a character in the range a-z, A-Z (i.e., any character lower case or upper)
    [a-zA-Z0-9_] - match a character in the range a-z, A-Z, 0-9, _ (i.e., any character lower case or upper, digits, and/or underscores)
    * - match the last expression (i.e., the character class [a-zA-Z0-9_]) zero or more times
    ) - end capture group 2
    ~ - end of regular expression

    I used ~ instead of / because I find they are more practical in regular expressions, as you need to match / pretty often so then you'd need to escape it; with ~ you don't have that problem. Feel free to substitute them for / if you wish.


    HTH
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for both explanations. It's very helpful.

    Works like a charm!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •