theateist
theateist

Reputation: 14399

How to write regex that match start with a letter, can contain letters, numbers, hyphens and 1 underscore?

I need to write regex(.NET) that match string: start with a letter, can contain letters, numbers, periods, hyphens, 1 underscore and follow '@'. I tried the follow one, but it doesn't match 'a_bc12@' for example.

How to fix it?

^[A-Za-z][A-Za-z0-9-]+_{0,1}(?=@)

EDITED: it should contain {3,10} length.

Upvotes: 1

Views: 5778

Answers (3)

Bergi
Bergi

Reputation: 664548

You will need to allow characters after the underscore again:

/^[A-Za-z][A-Za-z0-9\.-]*_?[A-Za-z0-9\.-]*(?=@)/

Also, I've added the periods and made the strings before and after the underscore optional.

Upvotes: 2

David M
David M

Reputation: 72870

Try this:

^[A-Za-z][A-Za-z0-9-\.]*_{0,1}(?=@)

Your use of + is one or more occurences; you want zero or more following the intial letter, which is *.

Upvotes: 0

Bonny Bonev
Bonny Bonev

Reputation: 131

Try to modify it as this:

[A-Za-z]+[A-Za-z0-9-/.]*_[A-Za-z0-9-/.]*@`

when you use + after expression, it means "one or more times" when you use * it means "zero or more times"

hope it helps.

Upvotes: 1

Related Questions