Integer in String with 1 or 2 digits

porszivo

sorry I don't know if you understand what I want to do and I am also not 100% sure if it can be done by regex, but maybe there is a better solution than the one I have in my mind.

I want to create a function which gets a string as a parameter and checks if there is an Integer in that string (this is the easy part) The function should return false, if there is an Integer, but with more than 2 digits or the digits are split.

Valid:

  • foo1bar
  • foobar1
  • f12obar
  • 12foobar

Invalid

  • f1o2obar
  • f1o23obar

So the easy part, regex for 1 or 2 digits is no big deal

[0-9]{1,2}

Another way, what I think is pretty bad code, is a loop through the whole String and count every int. As soon as I saw one and the next one is no int, every other int in that string will lead to an end.

I hope there is a smoother way to do this.

DeadChex

Replace all the non-numbers with nothing, then with that number see if the original string contains it.

String str = "foo1b2ar"; //starting string
String num = str.replaceAll("[\\D]",""); //the number it contains
return str.contains(num) && num.length() <= 2; //does the original have that number all together? and is it short?

Example:

"foo1bar" -> "1" -> Does the original contain "1"? Yes.

"f1o23obar" -> "123" -> Does the original contain "123"? No.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to format integer as string with 2 digits?

From Dev

Sorting digits in a string or integer?

From Dev

Converting string "1 + 2" to integer

From Dev

Long integer to string and vice versa, operation with digits

From Dev

Python - String of Digits to Integer using Recursion?

From Dev

Arrange of Integer Number in A string by their Sum of Digits

From Dev

C++ integer digits printed out in string?

From Dev

How to write a Regular Expression for integer 14 digits fix and first digit can be 1 or 2

From Dev

Convert vector<unsigned char> {1,2,3} into string "1-2-3" AS DIGITS

From Dev

Cast "1+2" string to integer value of 3 in PHP

From Dev

Check if integer has repeating digits. No string methods or arrays

From Dev

Extract/Remove portion of an Integer or string with random digits/characters in R

From Dev

all integers which are subsequences of an integer (considered as a string of digits)

From Dev

Joining a list of string digits to create an integer Python 3

From Dev

Converting large string of numbers (14 digits) to integer or long in C

From Dev

Extract/Remove portion of an Integer or string with random digits/characters in R

From Dev

all integers which are subsequences of an integer (considered as a string of digits)

From Dev

Using String.format to remove particular digits from an integer

From Dev

Generate random integer with ALL digits from 1-9

From Dev

How to get the last 2 digits of a number (integer or decimal)

From Dev

Is there library method to convert String "1, 2, 3, 4" or "1 2 3 4" to array of integers or List<Integer>?

From Dev

Convert an int to a string with 2 digits in C++

From Dev

RegEx to match 2 or more digits in a string

From Dev

Convert an int to a string with 2 digits in C++

From Dev

RegEx to match 2 or more digits in a string

From Dev

How to match 2 digits in python with string?

From Dev

Bash script regex match 1 or 2 digits

From Dev

Bash script regex match 1 or 2 digits

From Dev

Javascript check for 1 special character and 2 digits

Related Related

  1. 1

    How to format integer as string with 2 digits?

  2. 2

    Sorting digits in a string or integer?

  3. 3

    Converting string "1 + 2" to integer

  4. 4

    Long integer to string and vice versa, operation with digits

  5. 5

    Python - String of Digits to Integer using Recursion?

  6. 6

    Arrange of Integer Number in A string by their Sum of Digits

  7. 7

    C++ integer digits printed out in string?

  8. 8

    How to write a Regular Expression for integer 14 digits fix and first digit can be 1 or 2

  9. 9

    Convert vector<unsigned char> {1,2,3} into string "1-2-3" AS DIGITS

  10. 10

    Cast "1+2" string to integer value of 3 in PHP

  11. 11

    Check if integer has repeating digits. No string methods or arrays

  12. 12

    Extract/Remove portion of an Integer or string with random digits/characters in R

  13. 13

    all integers which are subsequences of an integer (considered as a string of digits)

  14. 14

    Joining a list of string digits to create an integer Python 3

  15. 15

    Converting large string of numbers (14 digits) to integer or long in C

  16. 16

    Extract/Remove portion of an Integer or string with random digits/characters in R

  17. 17

    all integers which are subsequences of an integer (considered as a string of digits)

  18. 18

    Using String.format to remove particular digits from an integer

  19. 19

    Generate random integer with ALL digits from 1-9

  20. 20

    How to get the last 2 digits of a number (integer or decimal)

  21. 21

    Is there library method to convert String "1, 2, 3, 4" or "1 2 3 4" to array of integers or List<Integer>?

  22. 22

    Convert an int to a string with 2 digits in C++

  23. 23

    RegEx to match 2 or more digits in a string

  24. 24

    Convert an int to a string with 2 digits in C++

  25. 25

    RegEx to match 2 or more digits in a string

  26. 26

    How to match 2 digits in python with string?

  27. 27

    Bash script regex match 1 or 2 digits

  28. 28

    Bash script regex match 1 or 2 digits

  29. 29

    Javascript check for 1 special character and 2 digits

HotTag

Archive