4

I have written the regex below but I'm facing an issue:

^[^\.]*[a-zA-Z]+$

As per the above regex, df45543 is invalid, but I want to allow such a string. Only one alphabet character is mandatory and a dot is not allowed. All other characters are allowed.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

4 Answers4

6

Just add the digits as allowed characters:

^[^\.]*[a-zA-Z0-9]+$

See demo

In case you need to disallow dots, and allow at least 1 English letter, then use lookaheads:

^(?!.*\.)(?=.*[a-zA-Z]).+$

(?!.*\.) disallows a dot in the string, and (?=.*[a-zA-Z]) requires at least one English letter.

See another demo

Another scenario is when the dot is not allowed only at the beginning. Then, use

^(?!\.)(?=.*[a-zA-Z]).+$
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

You need to use lookahead to enforce one alphabet:

^(?=.*?[a-zA-Z])[^.]+$

(?=.*?[a-zA-Z]) is a positive lookahead that makes sure there is at least one alphabet in the input.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You can use this:

^[^.a-z]*[a-z][^.]*$

(Use a case insensitive mode, or add A-Z in the character classes)

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

you can add the first part of your regex which is ^[^.]* to the end to be like this

^[^.]*[A-Za-z]+[^.]*$

try this Demo

Nader Hisham
  • 5,214
  • 4
  • 19
  • 35