wood burning stoves
The moose likes Beginning Java and the fly likes RegEx - contains numbers but no letters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RegEx - contains numbers but no letters" Watch "RegEx - contains numbers but no letters" New topic

RegEx - contains numbers but no letters

Jerry Lee

Joined: Sep 09, 2004
Posts: 16
I tried for an hour and could figure it out. I could find a way around, but I'd like to know the answer.

I am reading a file and would like to remove lines as follows:
00:02:21,365 --> 00:02:24,027

So, please give a Java regular expression that will pick up a line that contains numbers, but it doesn't contain any letters. Therefore:

1) 00:02:21,365 --> 00:02:24,027 (Yes)
2) (an empty line) (No - because it doesn't have numbers)
3) whatever123 (No - because it has letters)

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11854

can you show us some of the things you've tried?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jerry Lee

Joined: Sep 09, 2004
Posts: 16
I tried this one it doesn't work.

After reading this page, I know why it doesn't work now:

Regular Expression Test Page:
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43905
Have you found the escape sequences which match digits? You should find them in the Java Tutorial link you posted. I think you want something like \d, but in a String literal you have to escape the \ like this "\\d".
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Originally posted by Jerry Lee:
I tried this one it doesn't work.

That regex matches one character that is a digit and isn't a letter. You need a regex that matches a whole line, and you need to do the two tests separately. One regex for a line of characters other than letters would be ...and for a line that contains a digit, You don't need to call matches() twice though, because you can state one of the requirements as a lookahead: Finally, if you use the matches() method, you don't need the '^' and '$' anchors at either end of the regex, but you do need the '$' in the lookahead, to make sure it checks the whole line.

By the way, here's a site with a better regex tutorial:

[ May 28, 2008: Message edited by: Alan Moore ]
Jerry Lee

Joined: Sep 09, 2004
Posts: 16
Thank you very much for the detailed info. This is really helpful.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: RegEx - contains numbers but no letters
It's not a secret anymore!