0

I would like to extract a text using Regex that is between 1 and 8 chars and does not contain more than 2 letters ([A-Za-z]).

For example:

Valid: "12A-32B" from the text "Register:12A-32B Index:A"
Invalid: "12 Index" from the text "Register:12 Index:A"

In this example, the extracted text should have the text "Register" as a boundary.

I tried using positive/negative lookahead but to no avail.

Thank you.

Daniel
  • 13
  • 5
  • From `Register:12A-32B Index:A` - why shouldn't e.g. `er:12`, or `32B I`? And why not `r:12 I` from the second? I assume it has to do with word boundaries (**but that's not stated in the question**). Going on: `:12 ` in the second? `:A` from the same? Why not those? – SamWhan Jun 12 '17 at 14:03
  • I edited the question stating the regex boundary. – Daniel Jun 13 '17 at 06:00

2 Answers2

2

Try this regex:

^(?!.*[A-Za-z].*[A-Za-z].*[A-Za-z])[A-Za-z0-9-]{1,8}$

This will match any string containing at most 8 numbers or letters, with a maximum of 2 letters appearing in the string.

You may need an additional step to extract the text inside your original text. You could try using this regex:

Register:(.*) Index

We could try using a single regex for everything, but it would be complicated. And it might be easier to just use two steps from your app layer.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

I found the solution

  1. Extract the text using the Register start delimiter

  2. Use this regex with the extracted text:

    ^(.{1,8}?)(?<![A-Za-z]{3})( |$)
    
Daniel
  • 13
  • 5