Q: Regex for patterns like [ABC], ABC and ABCxx where xx is a number I have a text whose length can vary between 1 and 1000. I am looking to get the following sub strings extracted from the text. 


*

*Sub string of the form ABCxx/ABCx where ABC are always english alphabets and x/xx is a number which can vary from 0 to 99 (the numeric length is either 1 or 2). The following regex does the job for me to extract this sub string - [a-zA-Z]{3}[0-9]{1,2}

*Sub string of the form <space>ABC<space>, ABC (last sub string/word in the text)and ABC (first sub string in the text). Basically here I am trying to find a 3 letter word delimited by spaces in the text.
For getting the above matches, I have the following regex's.
[ ][a-zA-Z]{3}[ ], [ ][a-zA-Z]{3} and [a-zA-Z]{3}[ ]



*Same as 2, but the three character string can also be in a box bracket like [ABC]. 


\[([a-zA-Z]{3})\]  

Since the patterns are more or less similar, is there anyway to combine all 5 of them ?
Eg: ABC catmat dogdog [rat] LAN45 eat HGF1 jkhgkj abc
Here valid matches are ABC, rat, LAN45, eat, HGF1, abc.

A: R = /
    \p{L}{3}\d{1,2}    # match 3 letters followed by 1 or 2 digits
    |                  # or
    (?<=\A|\p{Space})  # match start of string or a space in a pos lookbehind
    (?:                # begin a non-capture group
      \p{L}{3}         # match three letters
      |                # or
      \[\p{L}{3}\]     # match three letters surrounded by brackets
    )                  # end of non-capture group
    (?=\p{Space}|\z)   # match space or end of string in a pos lookahead
    /x                 # free-spacing regex definition mode


"ABC catmat dogdog [rat] LAN45 eat HGF1 jkhgkj abc".scan R
   #=> ["ABC", "[rat]", "LAN45", "eat", "HGF1", "abc"] 

This regex is conventionally written (not free-spacing mode):
R = /\p{L}{3}\d{1,2}|(?<=\A| )(?:\p{L}{3}\[\p{L}{3}\])(?= |\z)/

Now consider:
 "ABCD123 [efg]456".scan R
   #=> ["BCD12"]

I believe this is consistent with the statement of the problem, but if "BCD12" should not be a match if it is preceded by a letter or followed by a digit (here both apply), then the regex should be modified as follows.
R = /
    (?<=\A|\p{Space})  # match start of string or a space in a pos lookbehind
    (?:                # begin a non-capture group
      \p{L}{3}         # match three letters
      \d{,2}           # match 0, 1 or 2 digits      
      |                # or
      \[\p{L}{3}\]     # match three letters surrounded by brackets
    )                  # end of non-capture group
    (?=\p{Space}|\z)   # match space or end of string in a pos lookahead
    /x                 # free-spacing regex definition mode

"ABC catmat dogdog [rat] XLAN45 eat HGF123 jkhgkj abc".scan R
  #=> ["ABC", "[rat]", "eat", "abc"]

Notice that, in both regexes, I replaced \p{Space} with a space character. In free-spacing mode spaces are removed before the regex is parsed, so they must be written \p{Space}, [[:space:]], [ ] (a character class containing a space), \ an escaped space character or, if appropriate, \s for a whitespace character (which includes spaces, newlines, tabs and a few other characters). 

A: Thank you all for your answers. This regex did the trick for me. 
(\b[a-zA-Z]{3}([0-9]{1,2})?\b)
