This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "RegEx - contains numbers but no letters" Watch "RegEx - contains numbers but no letters" New topic
Author

RegEx - contains numbers but no letters

Jerry Lee
Greenhorn

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)

Thanks.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

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
Greenhorn

Joined: Sep 09, 2004
Posts: 16
I tried this one it doesn't work.
[0-9&&[^a-zA-Z]]

After reading this page, I know why it doesn't work now:
http://java.sun.com/docs/books/tutorial/essential/regex/char_classes.html

Regular Expression Test Page:
http://www.fileformat.info/tool/regex.htm
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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.
[0-9&&[^a-zA-Z]]

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:

http://www.regular-expressions.info/
[ May 28, 2008: Message edited by: Alan Moore ]
Jerry Lee
Greenhorn

Joined: Sep 09, 2004
Posts: 16
Thank you very much for the detailed info. This is really helpful.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: RegEx - contains numbers but no letters