regex - Ruby regular expression groups -
i trying match strings might represent ranges somewhere in document can't quite figure out 1 thing groups... have far:
/(^-?[0-9]+)(\.\.+)(-?[0-9]+$)/
which matches 1..10, -20...20, -01234567890...-999999999, etc. however, want second group ($2) have value if middle 3 digits instead of two. want like:
=~ -01234567890...-999999999 $1 = -01234567890 $2 = ... $3 = -999999999 =~ 1..10 $1 = 1 $2 = (empty because 2 dots instead of 3) $3 = 10
any way specify this, make group if it's value?
you can use:
(^-?[0-9]+)(?:(?:[.]{,2})|([.]{,3}))(-?[0-9]+$)
which put result in second group if 3 .
.
explanation:
?:
- non-capturing group.|
- or
note (?:[.]{,2})
non-capturing while ([.]{,3})
capture group.
Comments
Post a Comment