Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find an integer within a string

Tags:

People also ask

How do I find an integer from a string in regex?

Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression '[0-9]+' with re. findall() method. [0-9] represents a regular expression to match a single digit in the string.

How do you find a number in a string?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.

How do I find a character in a string in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


I'd like to use regex with Java.

What I want to do is find the first integer in a string.

Example:

String = "the 14 dogs ate 12 bones"

Would return 14.

String = "djakld;asjl14ajdka;sdj"

Would also return 14.

This is what I have so far.

Pattern intsOnly = Pattern.compile("\\d*");
Matcher makeMatch = intsOnly.matcher("dadsad14 dssaf jfdkasl;fj");
makeMatch.find();
String inputInt = makeMatch.group();
System.out.println(inputInt);

What am I doing wrong?