RegEx

J

Justin To

version:
1. abc 600-qwer
2. abc600-qwer
3. abc 6.0.0-pedfs
4. abc 6.12.23-45
5. abc 5.0.6.0
etc...

I want to be able to identify which version is version 6, version 5,
etc..

I'm not sure if a regex is the best way. I need to be able to say that
#1-4 are version 6 and #5 is version 5. But I need to be able to
identify versions 1-8. Also, what if I need to identify, specifically,
version 5.5 and 5.6?

Thanks
 
Y

yermej

version:
1. abc 600-qwer
2. abc600-qwer
3. abc 6.0.0-pedfs
4. abc 6.12.23-45
5. abc 5.0.6.0
etc...

I want to be able to identify which version is version 6, version 5,
etc..

I'm not sure if a regex is the best way. I need to be able to say that
#1-4 are version 6 and #5 is version 5. But I need to be able to
identify versions 1-8. Also, what if I need to identify, specifically,
version 5.5 and 5.6?

Thanks

str = ['abc 600-qwer', 'abc600-qwer', 'abc 6.0.0-pedfs', 'ab
 
Y

yermej

Sorry about that. Firefox decided it was time to update and then
posted what I had already typed.

str = ['abc 600-qwer', 'abc600-qwer', 'abc 6.0.0-pedfs', 'abc
6.12.23-45', 'abc 5.0.6.0']

versions = str.map {|v| v.gsub(/\D/, '')[0,1]}
=> ["6", "6", "6", "6", "5"]
That will get you the major version numbers. If you want minors, etc.,
you'll need a more specific format, I think. Otherwise, you couldn't
be sure that 'abc 600-qwer' is version 6, 60, or 600. If you assume
the major is a single digit:

str.map {|v| v.scan(/(\d)\.?(\d+)/)[0]}
=> [["6", "00"], ["6", "00"], ["6", "0"], ["6", "12"], ["5", "0"]]

If you know more specifics about the version format, you could get
better results; particularly if you need anything beyond the minor
number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top