Check If a String Contains at Least One Numeric Character in R

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 check if given username has at least one letter in ruby

As I understand you want to determine if the string has only letters and numbers and at least one letter. You could use the following regular expression:

r = /\A\p{Alnum}*\p{L}\p{Alnum}*\z/

This reads, "match a start-of-string anchor, followed by zero or more alphanumeric (Unicode) characters (letters or numbers), followed by a letter, followed by zero or more alphanumeric characters, followed by an end-of-string anchor".

"12abc34".match?(r) #=> true
"1234567".match?(r) #=> false
"=12abc3".match?(r) #=> false
"".match?(r) #=> false

Another way:

r = /\A(?=.*\p{L})\p{Alnum}*\z/

This reads, "match a start-of-string anchor, followed by a letter preceded by zero or more of characters, in a positive lookahead (which consumes no characters), followed by zero or more alphanumeric characters, followed by an end-of-string anchor".

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?

Test for numeric elements in a character string

Maybe there's a reason some other pieces of your data are more complicated that would break this, but my first thought is:

> !is.na(as.numeric(x))
[1] TRUE TRUE TRUE TRUE FALSE FALSE

As noted below by Josh O'Brien this won't pick up things like 7L, which the R interpreter would parse as the integer 7. If you needed to include those as "plausibly numeric" one route would be to pick them out with a regex first,

x <- c("1.2","1e4","1.2.3","5L")
> x
[1] "1.2" "1e4" "1.2.3" "5L"
> grepl("^[[:digit:]]+L",x)
[1] FALSE FALSE FALSE TRUE

...and then strip the "L" from just those elements using gsub and indexing.

Regex to check if string has at least 2 numbers and 1 capital letter

thanks for all the answers. Looked through them, and I figured out a pretty simple way to do it using the regular expression below. I edited it to allow for setting a length on the password, just change the 9 and 15 to your desired lengths.

/^(?=.*[A-Z])(?=.*\d.*\d)[^\s]{9,15}\$$/

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;}

Need a Regex that contains at least one number, zero or more letters, no spaces, min/max

This is a typical password validation with your requirements.

Note that this will also match 8-13 digits as well (but it is requested).

Ten million + 1 (and counting) happy customers ..

^(?=.*\d)[a-zA-Z\d]{8,13}$

Explained

 ^                     # Beginning of string
(?= .* \d ) # Lookahead for a digit
[a-zA-Z\d]{8,13} # Consume 8 to 13 alphanum characters
$ # End of string

check if string contains both number and letter (at least)

You can use lookahead assertions to check for existence of any digit and any letter as:

^(?=.*[a-zA-Z])(?=.*[0-9])


Related Topics



Leave a reply



Submit