Results 1 to 3 of 3

Thread: Regex Help

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Regex Help

    I should make time to learn some regex, as in cases like this http://regexlib.com/ does not help that much.

    Someone can give me the regex to matc a string, of which the last character will always be a number from 1 to 6?

    yui-t1 - no match
    yui-t1 - match
    yui-t2 - match
    yui-t3 - match
    yui-t4 - match
    yui-t5 - match
    yui-t6 - match
    yui-t7 - no match
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Regex Help

    If that's the only rule you care about, then:
    Code:
    .*[1-6]{1}$
    .* means "any character, as many times as you want."
    [1-6] defines any number between 1 and 6 (inclusive)
    {1} specifies that there should be just 1 of the number between 1 and 6
    $ denotes the end of the line

    So all together this means, "any number of any character, followed by a single number 1-6, and then the line ends."

    You can of course get more specific about the first part of the pattern, if you'd like.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Regex Help

    make sure you realize that the * operator in regular expressions also allows zero characters to be found; a better description might be zero or more of any character, whereas the + operator requires that one or more of any character is found. you could switch them around if you need a string to precede the number.

    and, like Samba mentioned, you can get much more specific about the formatting of the first part of that string (requiring the 'yui' [or any other sequence of 3 alphabet characters], requiring the dash, requiring a t [or any single alphabet character] to precede the number, etc). you can be as specific or as general as you want to be!

    finally, I would suggest this website if you want to first learn about regular expressions. the regular expression library isn't really all that helpful for learning; regular expressions are an incredibly difficult thing to get your head around sometimes ;)

Posting Permissions

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



Click Here to Expand Forum to Full Width