Regex to Check the String Contains Only Letter and Numbers But Not Only Numbers

regex to check the string contains only letter and numbers but not only numbers

Here are the components of the regex we're going to use:

  • ^ and $ are the beginning and end of the string anchors respectively
  • \d matches a digit
  • [a-zA-Z] matches a letter
  • [a-zA-Z\d] matches a letter or a digit
  • * is "zero-or-more" repetition

With these, we can now compose the regex we need (see on


Here's an explanation of the pattern:

from the beginning...  till the end
| |

The 3 parts are:

  • Maybe some digits as a prefix...
  • But then definitely a letter!
  • And then maybe some digits and letters as a suffix


  • Class, Anchors, and Repetition

Check if string contains ONLY NUMBERS or ONLY CHARACTERS (R)

you need to persist your regex

all_num <- "123"
all_letters <- "abc"
mixed <- "123abc"

grepl("^[A-Za-z]+$", all_num, perl = T) #will be false
grepl("^[A-Za-z]+$", all_letters, perl = T) #will be true
grepl("^[A-Za-z]+$", mixed, perl=T) #will be false

Regex: How to match a string that is not only numbers


This says lookahead for lines that do not contain all digits and match the entire line.

How do I match a pattern with alphabets AND numbers, but not if it contains only alphabets?

You could assert the start of the string ^, use a non capturing group (?: with an alternation to match either a digit \d or | one or more characters followed by a digit [a-z]+\d. This makes sure that you have at least 1 digit and that you could also start with characters.

Then for the last part of the regex you could match zero or more digits and characters [a-z\d]* until the end of the string $.

To match upper and lowercase characters you could use [a-z] with the case insensitive flag /i


let pattern = /^(?:\d|[a-z]+\d)[a-z\d]*$/i;const strings = [  "a1b2c3d4e5",  "1a2b3c4d5e",  "abcdefg",  "1234567"];strings.forEach((s) => {  console.log(s + " ==> " + pattern.test(s));});

Java String - See if a string contains only numbers and not letters

If you'll be processing the number as text, then change:

if (text.contains("[a-zA-Z]+") == false && text.length() > 2){


if (text.matches("[0-9]+") && text.length() > 2) {

Instead of checking that the string doesn't contain alphabetic characters, check to be sure it contains only numerics.

If you actually want to use the numeric value, use Integer.parseInt() or Double.parseDouble() as others have explained below.

As a side note, it's generally considered bad practice to compare boolean values to true or false. Just use if (condition) or if (!condition).

Regular expression detect that string contains both letters and numbers

To match 9 char strings that contain 2 consecutive letters and the rest is just digits, you may use


See the regex demo.


  • ^ - start of string
  • (?=.{9}$) - the string length must be 9 chars
  • \d* - zero or more digits
  • [a-zA-Z]{2} - 2 letters
  • \d* - zero or more digits
  • $ - end of string.

JS demo:

var strs = ['AB1234567', '09AR30253', '0912345JL', '123456789', 'AABBCCAAA'];var rx = /^(?=.{9}$)\d*[a-zA-Z]{2}\d*$/;for (var s of strs) {  console.log(s, "=>", rx.test(s));}

REGEX a-z 0-9 but not only numbers

The \D shorthand class means any non-digit symbol. You should remove it from the pattern (so that it becomes "^[A-Za-z0-9_-]{10,30}$") for the matches to return true, as 1 is a digit in 1regex to check the string contains only letter and numbers but not only numbers Check if string contains ONLY NUMBERS or ONLY CHARACTERS (R) Regex: How to match a strine333e.

If you want to place a restriction (the string cannot consist of only digits) use an anchored look-ahead:


Here is a demo

Or, a shortened version with i modifier making the pattern case-insensitive:


Related Topics

Leave a reply