Search code examples
phpregexletters

regexp for combination of letters,special characters and numbers


from the below set of rows i need

  1. rows with 3 letters
  2. rows with special characters
  3. rows with combination of numbers and special characters,letters

from below records..

OJH,
WV],
2V,
W.W,
V,
@A,
AL,
AS,
1234,
1,
23

i need to select OJH,WV],2V,W.W,V,@A etc..

ie, combination of letters and special characters,combination of letters and numbers and letters or digits combination greater than 3

i need to skip AL,AS,1234,1,23 etc..


Solution

  • If I understand you correctly, this should do it:

    ^(?=.*[A-Z])(?=.*\d).*$|^(?=.*[@\].])(?=.*\d).*$|^(?=.*[@\].])(?=.*[A-Z]).*$
    

    Using positive look-ahead it checks for lines containing

    • both letters and digits
    • both special characters and digits (in this case @, ] and . counts as special)
    • both special characters and letters

    Check this example at regex101.

    This assumes the combinations are tested one by one - no commas.

    Regards.

    Edit

    Missed the wrong number of letters option. This should do it:

    ^(?=.*[A-Z])(?=.*\d).*$|^(?=.*[@\].])(?=.*\d).*$|^(?=.*[@\].])(?=.*[A-Z]).*$|^[A-Z]$|^[A-Z]{3,}$
    

    See it here.