minimum number in a string should be 1 regex validation?

regex number range 1-12
regex number range 1-1000
regular expression length limit
regex validator
regex number of characters
regex match specific number
regex number of digits
regex any number of digits

I have a String which I need to match. Meaning it should only contains a number followed by space or just a number and minimum number should be 1 always. For ex:

3 1 2
1 p 3
6 3 2
0 3 2

First and third are valid string and all other are not.

I came up with below regex but I am not sure how can I check for minimum number in that string should be 1 always?

str.matches("(\\d|\\s)+")

Regex used from here

Just replace \\d with [1-9].

\\d is just a shorthand for the class [0-9].

This is a better regex though: ([1-9]\\s)*[1-9]$, as it takes care of double digit issues and won't allow space at the end.

java, First and third are valid string and all other are not. I came up with below regex but I am not sure how can I check for minimum number in that string should be 1  The Minimum and Maximum Length Attributes. One more way to restrict what passes as valid input for an element is to use the minlength and maxlength attributes. These set the minimum and maximum number of characters that need to be entered in an input element to make it valid.

Not everything can or should be solved with regular expressions.

You could use a simple expression like

str.matches("((\\d+)\\s)+")

or something alike to simply check that your input line contains only groups of digits followed by one or more spaces.

If that matches, you split along the spaces and for each group of digits you turn it into a number and validate against the valid range.

I have a gut feeling that regular expressions are actually not sufficient for the kind of validation you need.

4.9. Limit the Length of Text, You want to test whether a string is composed of between 1 and 10 letters from A to Z. However, using regular expressions to check text length can be useful in letters A–Z. You can modify the regular expression to allow any minimum or limiting the number of characters that can be matched by patterns that include  The next two characters should be a number, but the first character should be any number from 1-9 and the second character should be any number from 0-9. It should be zero or one white space character.

More Examples Of Regular Expressions, Valid numbers are: 1.0, .02 A number can have zero or more digits in front of a single period (.) and it Because the pattern can start anywhere in the string, so it would match the substrings "-1" and "1" respectively! There may be zero digits in front of the period, but then there must be at least one digit behind it: \.[0-​9]+. Given a string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression. The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).

Regex is part of the solution. But I don't think that regex alone can solve your problem.

This is my proposed solution:

private static boolean isValid(String str) {
    Pattern pattern = Pattern.compile("[(\\d+)\\s]+");
    Matcher matcher = pattern.matcher(str);

    return matcher.matches() && Arrays.stream(Arrays.stream(matcher.group().split(" "))
            .mapToInt(Integer::parseInt)
            .toArray()).min().getAsInt() == 1;
}

Pay attention to the mathing type: matcher.matches() - to check match against the entire input. (don't use matcher.find() - because it will not reject invalid input such as "1 p 2")

Example: Matching Numeric Ranges with a Regular Expression, RegexMagic—Generate regular expressions matching integer numbers and ranges Though a valid regex, it matches something entirely different. This character class matches a single digit 0, 1, 2 or 5, just like [0125]. As you can see, you need to split up the numeric range in ranges with the same number of digits,  Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters 592 How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

[PDF] Regular Expressions: The Complete Tutorial, In this tutorial, I will use the term “string” to indicate the text that Note that «1+1=​2», with the backslash omitted, is a valid regex. integer number indicating the minimum number of matches, and max is an integer equal to or greater than. I am trying to check text with a regular expression in iOS, and below is my code. My regular expression is accepting one word or number which it should be minimum 8 and maximum 16 with numbers or alphabetic.

Check if a given string is a valid number (Integer or Floating Point) in , Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach). In Set 1 Hence one of the regular expression for an integer number is Creates a matcher that will match input1 against regex Split the string into minimum parts such that each part is in the another string  The backslash-period that we added changes the meaning of the regex to: “Match any string that contains a sequence of one to any number of digits, followed by a decimal point.” By the way, we had to enter backslash-period because the period is a special character in regular expressions.

Regular Expressions in C#, In this article I will explain you about Regular Expressions in C#. Pattern #1 will match for strings other than those containing numbers from 0 to 9 The next pattern, [1-9], means that it should contain at least one integer between 1 and 9 Here is a more advanced example where we validate a URI  Approach: The idea is to use the Regular Expression to solve this problem. The following steps can be followed to compute the answer. Get the String. Create a regular expression to check valid SSN (Social Security Number) as mentioned below:

Comments
  • I came up with below regex... Is that so...
  • can any number be a double digit or negative number?
  • @shmosel sorry let me add your link in the question.wanted to make it simple so thats why i didnt added your link. will do now
  • @Kartik no it can't.
  • Are you trying to match 3 digits in a row?
  • What about "10 3 5" then?
  • @GhostCat I was in the middle of creating a better regex. I've added that to the answer. :)
  • is it \s? not sure
  • @flash would be double slash when you put it in a java string.. single otherwise
  • The OP just confirmed that he has numbers to check, not digits. So excluding 0 doesn't work at all.