DaBears
DaBears

Reputation: 1743

Regular Expression option for 3 or less characters

I have a regular expression below that works for a string example like:

But now I'm trying to figure out how to change the regular expression to work for the examples above if they were like:

They can also be like

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

Answers (3)

Nikola Malešević
Nikola Malešević

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:

  • Customer: MCCOY, SMITH-JOHNSON, JAKE MCCOY
  • Id1: 5H, 3H
  • Id2: L24, R32, L-ABC, R-123, L-C2
  • Date: 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

zb226
zb226

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

Try it yourself on Regexr.

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

MikeFHay
MikeFHay

Reputation: 9013

\w{1,3}

matches 1 to 3 alphanumeric characters.

http://msdn.microsoft.com/en-us/library/az24scfc.aspx#quantifiers

Upvotes: 3

Related Questions