A
A
Arthur2016-02-18 10:47:15
Java
Arthur, 2016-02-18 10:47:15

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

2 answer(s)
S
Saboteur, 2016-02-18
@antoart

https://regex101.com/r/yD1kR3/1
(.*[0-9]+.*)

S
Sergey Gornostaev, 2016-02-18
@sergey-gornostaev

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 question

Ask a Question

731 491 924 answers to any question