• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex pattern to match 2.9.1, 2.9.2, 3.9.1, 3.9.2 and onwards  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i try [2-9][1-9][1-9], in that case 2.1.1 will match, but i want to match the only conditions mentioned in the subject
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well your second number should not match 1-9, it should only match 9.

And your third number should not match 1-9, it should only match 1-2 (if that is what you meant).
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the real problem is that the following should match
2.9
2.9.1
2.9.2
and so on
3.1
3.1.1
3.2.1
so from 3 onwards any digit could match
but for ones starting with 2, the only digit allowed next is 9
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that is somewhat more complicated!

I would write a regular expression that matches only numbers that begin with 2, and then write a separate one that matches only numbers beginning with 3. I believe you can then combine them with a boolean or operator.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The starting string is Android/2.9.1
so i tried to do this but it is not printing the right result
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because that regular expression requires the second digit to be 9, but you only require that if the first digit was a 2.

Try what I suggested above.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How will you combine it..this doesn't work
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You combine them with the boolean or operator (the pipe symbol)

So "(b|c)at" will match bat or cat, but not tat.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike, this helped. I didn't know about how we could merge two regex patterns.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that the dot is a metacharacter in regex and matches any character. To match a literal dot, you need to escape it.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you have a semantic version numbering system and you are defining a range of versions.

I just have to raise the question : Is using a regular expression the right tool here?
What are you trying to accomplish with this?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!