aspose file tools*
The moose likes Beginning Java and the fly likes Extract a number using regular expression? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extract a number using regular expression?" Watch "Extract a number using regular expression?" New topic
Author

Extract a number using regular expression?

H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381
Hi,

I have a string, for example, There are 365 days in a year on earth!
I wanted to extract the number 365.

I loop thru the string and test if a character is a digit, I concat it, so at the end I got 365.
Now, how do I do it using regular expression?

1M Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

\d means one digit
\d+ means one or more digits
() means capture that group

Can you build a regular expression using this information?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

If your String always contains only one (1) group of digits, you could approach this from the opposite end and
remove all non-digits with the Regex \D

If your look at the methods that the String class has, you'll find nice practical ones for both approaches.


Knowledge Reigns Supreme
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Jon Avadis wrote:If your String always contains only one (1) group of digits, you could approach this from the opposite end and
remove all non-digits with the Regex \D

Which would return 1252365 for the sentence "A year has 12 months, 52 weeks and 365 days". I doubt that's what you want. The regex Jeanne was hinting at will allow you to, with a loop, get 12, 52 and 365 all separately.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Avadis
Ranch Hand

Joined: Jul 20, 2011
Posts: 49

Rob Spoor wrote:
Jon Avadis wrote:If your String always contains only one (1) group of digits, you could approach this from the opposite end and
remove all non-digits with the Regex \D

Which would return 1252365 for the sentence "A year has 12 months, 52 weeks and 365 days". I doubt that's what you want. The regex Jeanne was hinting at will allow you to, with a loop, get 12, 52 and 365 all separately.


I fully agree that Jeanne's suggestion is more flexible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

I missed that red part
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381


I'll look at the group option.
H Paul
Ranch Hand

Joined: Jul 26, 2011
Posts: 381
With group



I got
365
1

But if the text="There are 365 days on earth! from day 1. From day 1 there are 365 days.";
then with the same pattern="(\\D*)(\\d+)(\\D*) (\\d+)";

I did not get
365
1
1
365

Is there a way to change the grouping only once so that it will work dynamically for all strings? because without group like pattern="\\d+" is OK.
Maarten Bodewes
Greenhorn

Joined: Aug 04, 2011
Posts: 14
Try Matcher.find() multiple times using "\\d+" as input...
David Scherfgen
Greenhorn

Joined: Nov 29, 2012
Posts: 1
I have recently started building a website that will collect regular expressions people frequently ask for.
My first article covers the (simple) problem of matching numbers. It discusses matching integer numbers and decimal fractions (also with E notation).

The article can be found here: Regular Expression for Numbers

There's also one about IP matching with Regex.

PS: Do you like my mascot "Tyrannosaurus Regex"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extract a number using regular expression?
 
Similar Threads
string extraction using regular expressions
use of regular expression in java
Need help in regular expressions
regular exression for concatenating string in 2 different lines into the same line
regular expression help