0

I am learning regex and I started my example with below exercise.

A regex with

  1. Min one lower case.
  2. Min one number

and I tried below and it works perfectly and already tested in regex exe.

^[0-9]+[a-z]+$

enter image description here

The only problem is: I can't write chars first and number after chars. I meant when I tried with example a1, it got failed

Can you please suggest? How can I write char first and then number using same regex

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Try `(^[0-9]+[a-z]+$|^[a-z]+[0-9]+$)`. See [and/or operator in regular expression](http://stackoverflow.com/questions/8020848/and-or-operator-in-regular-expression) – Irfan434 May 14 '17 at 12:10
  • @Irfan434 You above expression will not work for such string `ae11ss` – Sahil Gulati May 14 '17 at 12:21

3 Answers3

0

If your Regex Coach supports positive lookahead then this will help you out.

Regex demo

Regex: ^(?=.*?\d)(?=.*?[a-z]).*

1. ^ start of string

2. (?=.*?\d) positive look ahead for a digit

3. (?=.*?[a-z]) positive look ahead for a lowercase character a-z

4. .* match all

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • `?=.*?` can you please explain the meaning of this thing? – Pankaj May 14 '17 at 12:24
  • `.*` is matches all because it is greedy, where as `.*?` makes this operator as lazy. `.*?[a-z]` match all till the first occurence of `a-z` – Sahil Gulati May 14 '17 at 12:25
  • @Pankaj try this greedy one https://regex101.com/r/vVS8cZ/1/ and a lazy one https://regex101.com/r/uqD4KZ/1/ – Sahil Gulati May 14 '17 at 12:29
  • @Pankaj I think examples, in itself explain everything – Sahil Gulati May 14 '17 at 12:34
  • if I need `Min one lower case` , `Min one number` and `Min one upper case` then I will need 9 possible combinations using this way. right? – Pankaj May 14 '17 at 17:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144192/discussion-between-sahil-gulati-and-pankaj). – Sahil Gulati May 14 '17 at 17:30
  • @Pankaj I can help you with that as well.., we can discuss it over chat, for your above question you can check this https://regex101.com/r/nDvb69/3 – Sahil Gulati May 17 '17 at 16:01
0

While Sahil Gulati's answer is generally the better option, a more intuitive alternative would be to simply use the regex logical 'or' (the | symbol):

^[0-9]+[a-z]+$|^[a-z]+[0-9]+$

This matches either

  1. ^[0-9]+[a-z]+$
  2. ^[a-z]+[0-9]+$
henrikl
  • 479
  • 2
  • 14
  • if I need Min `one lower case` , `Min one number` and `Min one upper case` then I will need 9 possible combinations using this way. right? – Pankaj May 14 '17 at 12:20
0
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?=\S+$).{8,}$

Explanation

^                 # start-of-string
(?=.*[0-9])       # a digit must occur at least once
(?=.*[a-z])       # a lower case letter must occur at least once
(?=.*[A-Z])       # an upper case letter must occur at least once
(?=.*\W)          # a special character must occur at least once
(?=\S+$)          # no whitespace allowed in the entire string
.{8,}             # anything, at least eight places though
$                 # end-of-string
Pankaj
  • 9,749
  • 32
  • 139
  • 283