0

I have a validation to be done. The below pattern(post Man/PostMan/post Man etc) should not be allowed in my text box.
[P|p](OST|ost).*\s*[M|m][a|A][N|n]\s*(\d.)*

I have to add a few more validations to the same input such as: Allow:

  • Alphabets, Numbers and the special character "-"

And, must not allow:

  • Any other special characters
  • The above written pattern, anywhere I enter in the text box.

So the input

  • I am waiting for postMan
  • I am waiting for postMan ###
  • Post Man has not come , are invalid

whereas,

  • I am waiting for 123348-12
  • I hope my question is understandable now, are valid

I have tried the following:

([a-z 0-9 A-Z \\-])\*((?!\b[P|p]\*(OST|ost)\*\\.*\s\*[M|m][a|A][N|n]\s\*(\d.)\*\b))\*([a-z 0-9 A-Z])

But It stops working once it finds a character that does not match. For example:

asdasd 666 # posttt -> it stops validating anything after the special character.

What should my regex pattern be?

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
user3223509
  • 139
  • 1
  • 1
  • 12

1 Answers1

3

Description

^(?!.*?[Pp]*(OST|ost)*\.*\s*[Mm][Aa][Nn]\s*(\d.)*)[a-z0-9A-Z-]*$

Regular expression visualization

** To see the image better, simply right click the image and select view in new window

This regular expression will do the following:

  • not allow your first expression to match anywhere in your string
  • will then only allow a-z, A-Z, 0-9, and -

I would like to point out that if your string in only going to allow letters, numbers, and a hyphen then the test in your expression for a dot is not necessary. Also the multiple upper and lower case character sets can be removed by using the case insensitive flag.

Example

Live Demo

https://regex101.com/r/oY0hK2/1

Sample text

aWoeed1#fde39393aii
aWoeed1fde39393AII
aWoeed1fde39393AIIpostman 3a

Sample Matches

aWoeed1fde39393AII

Explanation

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    [Pp]*                    any character of: 'P', 'p' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (                        group and capture to \1 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      OST                      'OST'
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      ost                      'ost'
--------------------------------------------------------------------------------
    )*                       end of \1 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \1)
--------------------------------------------------------------------------------
    \.*                      '.' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    [Mm]                     any character of: 'M', 'm'
--------------------------------------------------------------------------------
    [Aa]                     any character of: 'A', 'a'
--------------------------------------------------------------------------------
    [Nn]                     any character of: 'N', 'n'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    (                        group and capture to \2 (0 or more times
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
      .                        any character except \n
--------------------------------------------------------------------------------
    )*                       end of \2 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \2)
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-z0-9A-Z-]*            any character of: 'a' to 'z', '0' to '9',
                           'A' to 'Z', '-' (0 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43