tags:

views:

45

answers:

2

I have the following string "Downloaded: 1 files, 8.7K in 0s (16.9 MB/s)" which I got from wget and want to pattern match it by making a regular expression of it.

I tried with:

/^Downloaded: ([0-9]*) files, ([0-9GK]*) in ([0-9.]*)s ([0-9.]) [KM]B\/s/ 

But it does not work. I would appreciate any help. Thanks!

+1  A: 

A character class like [0-9] only matches a single character. If you want one or more you should specify [0-9]+. Also, you forgot the dot in the character class for the size.

Also you could use \d+ instead of [0-9]+.

Christoffer Hammarström
+2  A: 

"Downloaded: 1 files, 8.7K in 0s (16.9 MB/s)"

does not match /^Downloaded: ([0-9]) files, ([0-9GK]) in ([0-9.]*)s ([0-9.]) [KM]B\/s/ because of the decimal point in "8.7K", and the parentheses around the download speed. Change that to:

/^Downloaded: (\d+) files, ([0-9.]+[GMK]) in ([0-9.]+)s \(([0-9.]+) [KM]B\/s\)/

I also made it so some of these values can have more than one digit, and you can have files that are in the megabyte range as well as kilobyte and gigabyte.

Ether
Thank you guys. I just noticed that the regexp I submitted here was not the exact one I tested. I actually knew about the + function and thought about the byte range alternatives. But you made it work for me. Thanks!
Andreas Sjöström
@Andreas: So mark this answer accepted then.
Christoffer Hammarström
I would love to do that. But how?
Andreas Sjöström
ok. I found out by myself :)
Andreas Sjöström