1

I have the regex ^[A-Za-z]{2}[A-Za-z0-9]{9}$ to validate the format of a string AB987654321

if (!( $address -match "^[A-Za-z]{2}[A-Za-z0-9]{9}$"))

    {
     write-host $address
    }

I would like to modify this regex so that I would be able to validate smtp:[email protected]

So the string should start with smtp: or SMTP:
Then the above format '^[A-Za-z]{2}[A-Za-z0-9]{9}$'
anything after @

Could someone please help?

2
  • 4
    Then what about ^smtp:[a-z]{2}[a-z0-9]{9}@.+ if you don't care about the domain part. -match is case insensitive, so you don't need [a-zA-Z] Commented Jul 22, 2020 at 18:50
  • Why do you need help with this, you've explained all the pieces, just put them together. What have you tried ? Commented Jul 22, 2020 at 21:02

2 Answers 2

1

Match your new string format using

$string -match '^smtp:[A-Z]{2}[A-Z0-9]{9}@'

See proof. As Theo commented, you do not need [A-Za-z], [A-Z] will suffice. Note the -match does not require the pattern to match the entire string, so you need nothing after @.

EXPLANATION:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  smtp:                    'smtp:'
--------------------------------------------------------------------------------
  [A-Z]{2}                 any character of: 'A' to 'Z' (2 times)
--------------------------------------------------------------------------------
  [A-Z0-9]{9}              any character of: 'A' to 'Z', '0' to '9'
                           (9 times)
--------------------------------------------------------------------------------
  @                        '@'
Sign up to request clarification or add additional context in comments.

Comments

1

Try this patter

^smtp\:[a-z]{2}[a-z0-9]{9}[@]\w+[.]\w+

Regex Demo

1 Comment

@AdminOfThings thanks for the feedback i have made some changes to make it less verbose let me know if there should be anymore changes or feel free to edit the answer thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.