1

How can i make a regex that string should contain char and number. if its just letter or just number it should return me false

Eg:

123swift -> true
swift123 -> true
1231 -> false
swift -> false

My regex:

[a-z]|[0-9]
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
Swifty
  • 75
  • 1
  • 8
  • Thank you for updating your question to be more specific. You can use `^(?:[a-z]+\d+|\d+[a-z]+)$` (change `[a-z]` to `[a-zA-Z]` if uppercase variants are also needed). Alternatively, if you just need to ensure that each character set exists (but allow your regex to match any characters like `.`), you can use `^(?=[^a-z]*[a-z])(?=\D*\d)` – ctwheels Jul 17 '20 at 15:12

1 Answers1

2

Use

^(?=.*?[A-Za-z])(?=.*?[0-9])[0-9A-Za-z]+$

Or, a presumably more efficient version:

^(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])[0-9A-Za-z]+$

See proof.

Expanation:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    [A-Za-z]                 any character of: 'A' to 'Z', 'a' to 'z'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [0-9A-Za-z]+             any character of: '0' to '9', 'A' to 'Z',
                           'a' to 'z' (1 or more times (matching the
                           most amount possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
  • 1
    `.*?` is not efficient. Instead, use `[^a-zA-Z]*` and `\D*` (and taking `\n` into account, use the following). `^(?=[^a-zA-Z\n]*[A-Za-z])(?:[^\d\n]*[0-9])[0-9A-Za-z]+$` is much more efficient that your current pattern. – ctwheels Jul 20 '20 at 14:43