The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Regular Expression to Check If String contains at least 1 Numeric Digit

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
Java Regular Expression to Check If String contains at least 1 Numeric Digit Posted: Jan 18, 2014 9:53 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: Java Regular Expression to Check If String contains at least 1 Numeric Digit
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
This week's task is to write a regular expression in Java to check if a String contains any digit or not. For example, passing "abcd" to pattern should false, while passing "abcd1" to return true, because it contains at-least one digit. Similarly passing "1234" should return true because it contain more than one digit. Though java.lang.String class provides couple of methods with inbuilt support of regular expression e.g.split method, replaceAll() and  matches method, which can be used for this purpose, but they have a drawback.  They create a new regular expression pattern object, every time you call. Since most of the time we can just reuse the pattern, we don't need to spend time on creating and compiling pattern, which is expensive compare to testing an String against pattern. For reusable patterns, you can take help of java.util.regex package, it provides two class Pattern and Matcher to create pattern and check String against that pattern. In order to complete this, we first need to create a regular expression pattern object, we can do that by passing regular expression String "(.)*(\\d)(.)*" to Pattern.compile() method, this returns a compiled version of regular expression String. By using this pattern you can get Matcher object to see if input string passes this regular expression pattern or not. We will learn more about our regular expression String in next section, when we will see our code example for check if String contains a number or not.
Read more »

Read: Java Regular Expression to Check If String contains at least 1 Numeric Digit

Topic: Android Service Tutorial Previous Topic   Next Topic Topic: Why Your Boring Data Will Outlast Your Sexy New Technology

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use