Reputation: 23
I have item numbers in the general form: 1-3 digits followed by optional lower case character followed by optional single digit followed by a word followed by optional single digit.
For example, all of the following are legitimate item numbers: "21aRepair", "04iMod2", "04d1RR", "5", "07", "8a", "09b", "04g3"
I need to break these item numbers up into components of the form: Component 1) 1-3 digits followed by optional lower case character followed by optional single digit Component 2) a word Component 3) optional single digit
For example:
"21aRepair"
should become "21a", "Repair", null
"04iMod2"
should become "04i", "Mod", "2"
"04d1RR"
should become "04d1", "RR", null
"5"
should become "5", null, null
I have the following regex: (^\d{1,3}[a-z]?\d?)
that works fine for component 1. I have tried several options for component 2 without success.
I thought the following: (?<=^\d{1,3}[a-z]?\d?)([a-zA-Z]*\d?)
would work. Adding the lookbehind "(?<="
to the component 1 pattern should say "take whatever is after component 1", and the group ([a-zA-Z]*\d?)
should match components 2 and 3. This does not yield the expected results. Can someone tell me where I am going wrong?
Upvotes: 2
Views: 77
Reputation: 626802
You may consider using
^(\d{1,3}[a-z]?\d?)([a-zA-Z]*)(\d?)$
See the regex demo
Details:
^
- start of string(\d{1,3}[a-z]?\d?)
- Group 1: component, 1 to 3 digits, an optional lowercase ASCII letter and an optional digit([a-zA-Z]*)
- Group 2: word, zero or more ASCII letters(\d?)
- Group 3: an optional digit$
- end of stringIn C#, use
var m = Regex.Match(str, @"^(\d{1,3}[a-z]?\d?)([a-zA-Z]*)(\d?)$");
check if m.Success
is true, and access the three groups using m.Groups[n].Value
where n
is the group ID.
Upvotes: 3