Reputation: 1743
I have a regular expression below that works for a string example like:
MCCOY 3H L24 FINAL 02-28-2012.dwg
orSMITH-JOHNSON 5H R32 FINAL 05-26-2012.dwg
But now I'm trying to figure out how to change the regular expression to work for the examples above if they were like:
MCCOY 3H L-ABC FINAL 02-28-2012.dwg
orSMITH-JOHNSON 5H R-123 FINAL 05-26-2012.dwg
They can also be like
MCCOY 3H L-C2 FINAL 02-28-2012.dwg
or SMITH-JOHNSON 5H R-2 FINAL 05-26-2012.dwg
So to sum this up, that middle section will always have a Alphabetic character followed by a Dash and then it could have as much as 3 numbers or alphabetic characters or as few as 1 number or alphabetic character.
"^[a-z]+(?:[ -][a-z]+)*\s+\d+[a-z]\s+[a-z]\d+\s+[a-z]+\s+\d{2}-\d{2}-\d{4}\.dwg$"
Upvotes: 0
Views: 5999
Reputation: 1858
This is the regex that will return all the fields separately:
^(?<Customer>.*?)\s+?(?<Id1>[\-a-z0-9]*?)\s+?(?<Id2>[\-a-z0-9]*?)\s+?FINAL\s+?(?<Date>[\-0-9]{10})\.dwg$
Fields description:
MCCOY
, SMITH-JOHNSON
, JAKE MCCOY
5H
, 3H
L24
, R32
, L-ABC
, R-123
, L-C2
02-28-2012
, 05-26-2012
Tested on:
MCCOY 3H L24 FINAL 02-28-2012.dwg
SMITH-JOHNSON 5H R32 FINAL 05-26-2012.dwg
JAKE MCCOY 3H L-ABC FINAL 02-28-2012.dwg
SMITH-JOHNSON 5H R-123 FINAL 05-26-2012.dwg
MCCOY 3H L-C2 FINAL 02-28-2012.dwg
SMITH-JOHNSON 5H R-2 FINAL 05-26-2012.dwg
Upvotes: 1
Reputation: 10500
I needed to add a (?i)
to get this to work because [a-z]
does not match upper-case characters - at least in the regexp engines I normally use :) So I arrived at this:
(?i)^[a-z]+(?:[ -][a-z]+)*\s+\d+[a-z]\s+[a-z]-\w{1,3}\s+[a-z]+\s+\d{2}-\d{2}-\d{4}\.dwg
Edit: As @Oded made me notice, the stars in the question are not part of the input strings - removed and updated Regexr link.
Upvotes: 2
Reputation: 9013
\w{1,3}
matches 1 to 3 alphanumeric characters.
http://msdn.microsoft.com/en-us/library/az24scfc.aspx#quantifiers
Upvotes: 3