Answer the question
In order to leave comments, you need to log in
How to find a string containing numbers using a regular expression in Java?
Good afternoon!
There is a task to find all words that contain numbers.
For example, f7df, 74sv, pet3ya
Please tell me a regular expression to search for such words.
I do not quite understand how to write an expression in which I do not know the exact location of numbers and letters.
Please help.
Thanks in advance for the replies.
Answer the question
In order to leave comments, you need to log in
import java.util.regex.*;
public class RegexDemo {
public static void main(String[] args) {
String text = "f7df, qqq, www, 74sv, zzz, пет3я";
Pattern p = Pattern.compile("\\b\\w*\\d+\\w*,?\\b");
Matcher m = p.matcher(text);
while(m.find()) {
System.out.println(text.substring(m.start(), m.end()));
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question