File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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)?


Kishore
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 http://www.fileformat.info/tool/regex.htm.
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Regular expression for numeric copy