PDA

Click to See Complete Forum and Search --> : Regex Help


StrangerInBeijing
Dec 29th, 2009, 10:44 PM
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

SambaNeko
Dec 30th, 2009, 01:39 AM
If that's the only rule you care about, then:

.*[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.

kows
Dec 30th, 2009, 10:48 AM
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 (http://regularexpressions.info/) 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 ;)