File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Regular expression for numeric copy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression for numeric copy" Watch "Regular expression for numeric copy" New topic

Regular expression for numeric copy

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
If i have a string with both numeric and alpha characters, how do i copy only the numeric values in to a new string from original(using regex)?

SCJP, blog
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

That would be (\d+)[\i] or [i]"(\\d+)" as a Java-string.

If I give it the string "abc123def456ghi", I get 123 in group 0 and 456 in group 1.

You can test your regexp online at
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
The easiest way would be to replace all non-digits from the original String:

[ September 04, 2008: Message edited by: Piet Verdriet ]
I agree. Here's the link:
subject: Regular expression for numeric copy
It's not a secret anymore!