tags:

views:

336

answers:

4

Regex to Find Second Char is Alpha up to 5 Alpha Followed by 1 numeral.

Thanks

A: 
.\w{1,5}\d

any character followed by between 1 and 5 letters then 1 number

Unkwntech
Yep thats the l33t h4x0r way ;)
Byron Whitlock
The {1} is unnecessary.
titaniumdecoy
@titaniumdecoy, that is true, not sure what I was thinking
Unkwntech
This one constrains the first character although a constraint is not specified in the question.
brian d foy
It would only do that if I had used ^\w{1,6}\d which would have specified the beginning of the line, but \w{1,6}\d will match anywhere in the string think of "12lsdfasdfg1" the match here is: asdfg1, however we are left without any information to what the data looks like so we are all just guessing.
Unkwntech
A: 

Double-checking...

  • 2nd character is alpha
  • up to 5 following alpha (i.e., total 1 - 6 alpha)
  • final numeric

Yes?

Assuming first character is irrelevant:

/.[A-Za-z]{1,6}\d/
Jonathan Lonowski
Did you mean {1,5}?
JaredPar
I clarified {1,6} in the checklist. If the "up to 5" includes the first alpha, then it should be {1,5}. But, I read each part as exclusive -- 1 + (0,5) => 1,6
Jonathan Lonowski
A: 

This should do the trick. Regular expression language is .Net implementation

^.[a-zA-Z]{1,5}\d$

Breakdown

  • ^ force the match to start at the begining of the text
  • . will match anything
  • [a-zA-Z]{1,5} will match any character a-z at least one time but no more than five. Because of the preceeding "." this means that the match will start at the second character
  • \d matches a single digit
  • $ matches the end of the text
JaredPar
The "$" character is a "line end assertion". To get the "line begin assertion", use the "^" character.
benjismith
Yeah, did it backwards
JaredPar
+1  A: 

I was not successful in implementing any of the solutions above, probably my poor explanation of need. I did solve it in code not using Regex. Thanks to everyone who took the time to help. For those that thought this was homework, it was not.

Here is some sample data.

Need this

I INDY2 ' INDY VECTOR DP FOR FILING '041802 REM 59268640 I JODUB3 ' AIRWAY FOR JODUB SID '051205 CLW 59268649

Don't need this

I J149 ' GDK 59265224 I APE074 ' 43092 REF 59265777

This is how I tested in code.

Dim IsSidStar As Boolean = False
        If aAirways.Name.Length > 2 Then
            Dim a2ndChar As Char = aAirways.Name(1)
            Dim alastChar As Char = aAirways.Name(aAirways.Name.ToString.Length - 1)
            Dim a2ndlastChar As Char = aAirways.Name(aAirways.Name.ToString.Length - 2)

            If Char.IsLetter(a2ndChar) = True AndAlso Char.IsNumber(alastChar) = True AndAlso Char.IsNumber(a2ndlastChar) = False Then
                IsSidStar = True
            End If
        End If