SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    what does these signs means

    hi

    i know ([a-zA-Z]+) matches any characters lower or uppercase

    But i would like to what do these below 2 signs means and what do they match ?

    1) ([a-zA-Z_]+)
    2) ([a-zA-Z_-]+)

    vineet

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,167
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    First one also matches underscores (the _ )
    Second one also matches underscores (the _ ) and the hypen (the - )

  3. #3
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    First one also matches underscores (the _ )
    Second one also matches underscores (the _ ) and the hypen (the - )
    hi cp

    is _ underscore sign optional in ([a-zA-Z_]+)

    vineet

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,167
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vinpkl View Post
    hi cp

    is _ underscore sign optional in ([a-zA-Z_]+)

    vineet
    All of them are optional.
    The + simply means one of the characters in found between [] needs to be found for the expression to be true, but it will capture all matching characters until the end or a non-matching character is found.\

    To put that into perspective, the following will be captured

    _
    a
    A
    z
    Z
    abc_
    _abc
    a_b_c
    etc

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thanks cp

    for the clarification

    vineet

  6. #6
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hi cp

    one more question :

    whats the difference in (.*) and (.+)

    vineet

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,167
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    The first is for matching any character zero or more times, the latter is matching any character 1 or more times

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,167
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    If you want an AWESOME book for Regular Expressions, I recommend O'Reilly Regular Expression Pocket Reference

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,085
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Also, regex basics are quite nicely explained on http://www.regular-expressions.info/
    A lot of stuff I know about regex I got from there.
    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

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    cp,

    You really should add that Apache advises that the metacharacter (within a character range definition), the "-", should be the first character within the range definition so it can be understood to be the character (rather than the metacharacter used to define a range of characters). Under common usage, however, Apache accepts the - as the last character in the range (as vineet has in his code snippet).

    WARNING: The references to the regex book and article are fine but mod_rewrite only uses a rather small and very basic subset of regex BECAUSE the Apache variables are not multi-line, they are not formatted (italic, bold, etc) and they must fit the format of the variable as required by Apache. That makes learning the subset used by mod_rewrite a relatively trivial task.

    As for the definitive book on regex, let me recommend what most consider the bible on this subject: Jeffrey Friedl's "Mastering Regular Expressions" which is published by O'Reilly.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  11. #11
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,167
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    As for the definitive book on regex, let me recommend what most consider the bible on this subject: Jeffrey Friedl's "Mastering Regular Expressions" which is published by O'Reilly.
    The reason I suggested the O'Reilly Regular Expression Pocket Reference is because it actually teaches you the regular expression syntax used by various languages (I think apache may be in there too, but I don't quite remember and my book is on my work desk). So you can see the C# implementation, versus PHP, Perl, Java, etc. It is very handy when you are trying to nail down a regular expression and can't quite remember some of the oddities a language may have implemented.

    Quote Originally Posted by dklynn View Post
    You really should add that Apache advises that the metacharacter (within a character range definition), the "-", should be the first character within the range definition so it can be understood to be the character (rather than the metacharacter used to define a range of characters). Under common usage, however, Apache accepts the - as the last character in the range (as vineet has in his code snippet).
    Even though this is in the URL Rewriting forum, I didn't want to assume it was meant for Apache, versus maybe .NET URL Rewrite, or just a regular expression to be used for some other purpose.


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
  •