Forum Moderators: open

Message Too Old, No Replies

Regular Expression Help

         

Argblat

7:17 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Can someone help me polish off this Regular Expression...there are some aspects I don't know, and I also fear that I might forget to include something important due to my lack of knowledge with RegEx

I will be testing against a single string, here are the rules:

1. string can be either 10 or 11 characters long (10 mandatory, 1 optional)

2. The first 4 characters must be [a-z] or [A-Z]

3. The next 6 characters (5-10) must be numeric [0-9]

4. The final character (which is optional) can be either 0-9 or the letter 'k' ([0-9][K][k]?)

Here is what I have so far, but it's incomplete:


string c = "ADXU220564";
if(Regex.IsMatch(c.ToLower(), @"[a-z]{4}\d{6}\w{1}"))

I appreciate the help ... Regular Expressions is something I could certainly benefit from learning more about, but unfortunatley I never get much time to spend learning

Thank you!
-Mike

Argblat

2:21 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



This one works, but I'm really hoping someone with Reg Ex experience can take a look at it and tell me if I'm making a mistake or missing something important


\b[a-z,A-Z]{4}\d{6}[0-9,K,k]{0,1}\b

if i'm in the wrong forum, can someone please suggest a better option

thank you
Mike

mattglet

8:17 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[webmasterworld.com...]

Those guys are usually quite sharp.

emsaw

8:20 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



How about:

^[a-zA-Z]{4}\d{6}[\dKk]?$

That's.. it must start and end with this entire sequence.. a 4 character sequence, followed by a 6 digit sequence, with an optional digit or the letter K.

HTH,

Mark

coffeebean

8:40 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



This one works ...
\b[a-z,A-Z]{4}\d{6}[0-9,K,k]{0,1}\b

Don't use commas as separators inside a character class (the stuff in square brackets). As it stands you're allowing a comma in the first four digits and in the 11th slot. So something like ADX,220564, would give a false positive match.

Write it as emsaw suggests with the stuff in square brackets all crammed together.

Also starting with ^ and ending with $ means the string being tested must start right before the thing you're looking for and end right after it. So ^[a-zA-Z]{4}\d{6}[0-9Kk]{0,1}$ wouldn't match a string like I got a great deal on a ADXA220564k. For that you'd need to use the word boundary meta characters you had previously, so \b[a-zA-Z]{4}\d{6}[0-9Kk]{0,1}\b

Argblat

9:38 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Thank you emsaw and coffeebean, I do believe I now have a solid solution ... and the comma issue is exactly why I decided to ask for some expert webmaster world advice in the first place. It so easy to make a stupid mistake when you dont' have the experience under your belt

mattglet, thank you for the suggestion, even though I didnt' need to use it I do appreciate the effort

-Mike

Bernard Marx

11:22 pm on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Those guys are usually quite sharp.

You won't find us guys over here [webmasterworld.com] taking a nap either.