Check if a string contains a number
You can use any
function, with the str.isdigit
function, like this
def has_numbers(inputString):
return any(char.isdigit() for char in inputString)
has_numbers("I own 1 dog")
# True
has_numbers("I own no dog")
# False
Alternatively you can use a Regular Expression, like this
import re
def has_numbers(inputString):
return bool(re.search(r'\d', inputString))
has_numbers("I own 1 dog")
# True
has_numbers("I own no dog")
# False
Regex: Check if string contains at least one digit
I'm surprised nobody has mentioned the simplest version:
\d
This will match any digit. If your regular expression engine is Unicode-aware, this means it will match anything that's defined as a digit in any language, not just the Arabic numerals 0-9.
There's no need to put it in [
square brackets]
to define it as a character class, as one of the other answers did; \d
works fine by itself.
Since it's not anchored with ^
or $
, it will match any subset of the string, so if the string contains at least one digit, this will match.
And there's no need for the added complexity of +
, since the goal is just to determine whether there's at least one digit. If there's at least one digit, this will match; and it will do so with a minimum of overhead.
How to detect if a string contains at least a number?
Use this:
SELECT * FROM Table WHERE Column LIKE '%[0-9]%'
MSDN - LIKE (Transact-SQL)
How do I check if a string contains at least one number, letter, and character that is neither a number or letter?
You can use Regex:
I took it from here: Regex for Password
var checkPassword = function(password){
return !!password.match(/^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #+=\(\)\^?&])[A-Za-z\d$@$!%* #+=\(\)\^?&]{3,}$/);
};
I use this Regex:
Minimum 3 characters at least 1 Alphabet, 1 Number and 1 Special Character:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%* #=+\(\)\^?&])[A-Za-z\d$@$!%* #=+\(\)\^?&]{3,}$"
This regex will enforce these rules:
At least one English letter, (?=.*?[A-Za-z])
At least one digit, (?=.*\d)
At least one special character, (?=.[$@$!% #+=()\^?&]) Add more if you like...
Minimum length of 3 characters (?=.[$@$!% #?&])[A-Za-z\d$@$!%* #+=()\^?&]{3,} include spaces
If you want to add more special characters, you can add it to the Regex like I have added '(' (you need to add it in two places).
And for those of you who ask yourself what are those two exclamation points, here is the answer: What is the !! (not not) operator in JavaScript?
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){
to:
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)
.
Regex to determine if string contains at least 2 numbers
Try this:
- .* matches everything (from 0 to n times)
- [0-9] is number from 0-9
Pattern pattern = Pattern.compile("[0-9].*[0-9]");
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
return true;
}
Related Topics
Issue When Comparing Result of To_Char(Mydate, 'Day') to a String
Memory Effective Way to Read Blob Data in C#/SQL 2005
Oracle SQL Syntax: Quoted Identifier
Why No Many-To-Many Relationships
What Are the Uses for Cross Join
Differencebetween Cube, Rollup and Groupby Operators
Oracle Get Checksum Value for a Data Chunk Defined by a Select Clause
How to Rewrite This SQL into Codeigniter's Active Records
Function to Get Number of Weekdays Between Two Dates Excluding Holidays
How to Select Records from Last 24 Hours Using SQL
How to Create Temp Table with Select * into Temptable from Cte Query
How to Store Decimal Values in SQL Server
Why am I Getting a "[Sql0802] Data Conversion of Data Mapping Error" Exception
Column Does Not Exist in the in Clause, But SQL Runs
Why Can't I Access My Cte After I Used It Once
How to Create Daylight Savings Time Start and End Function in SQL Server