'Regex matching plus or minus
Could someone please look at the following function and explain the regex for me as I don't understand it and I don't like using something I don't understand as then I won't be able to replicate it for use in the future and nor do I learn from it.
Also can someone explain the double !! in front, I know single means not so does double mean not "not"?
The function is a patch to String
to check if it's capable of being converted to an integer or not.
class String
def is_i?
!!(self =~ /\A[-+]?[0-9]+\z/)
end
end
The main thing that's giving me trouble is [-+]
as it makes little sense to me, if you could explain in the context given it would be very helpful.
EDIT: Since people missed the second part of the question I'll be a little more explicit.
What does !!
Mean in front of the check, I know a single !
means NOT but I can't find what !!
means.
Solution 1:[1]
The [-+]
Character Class
[-+]
is a character class. It means "match one character specified by the class", i.e. -
or +
.
Hyphens in Character Classes
I can see how this particular class can be confusing because the hyphen often plays a special role in a character class: it links two characters to form a character range. For instance, [a-z]
means "match one character between a
and z
, and [a-z0-9]
means "match one character between a
and z
or between 0
and 9
.
However, in this case, the hypen in [-+]
is positioned in a place where it cannot be used to specify a range, and the -
is just a literal hyphen.
Decoding the entire expression
- Assert position at the beginning of the string
\A
- Match a single character from the list “-+”
[-+]?
- Between zero and one times, as many times as possible, giving back as needed (greedy)
?
- Between zero and one times, as many times as possible, giving back as needed (greedy)
- Match a single character in the range between “0” and “9”
[0-9]+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
+
- Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Assert position at the very end of the string
\z
Solution 2:[2]
A Character Class defines a set of characters, any one of which can occur in a string for a match to succeed.
For example, the regular expression [-+]?[0-9]+
will match 123
, -123
, or +123
because it defines a character class (accepting either -
, +
, or neither one) as its first character.
In context:
\A
asserts position at the start of the string.[-+]
any character of:-
or+
(?
optional, meaning between zero and one time)[0-9]
any character of:0
to9
(+
quantifier meaning 1 or more times)\z
asserts position at the very end of the string.
What does !!
mean?
!!
placed together converts the value to a boolean.
Solution 3:[3]
explain the regex for me as I don't understand it
Pattern explanation: \A[-+]?[0-9]+\z
\A Start of string
[-+]? plus or minus sign [zero or one time (optional)]
[0-9]+ 0 to 9 any digit [one or more times]
\z End of string
The above regex pattern is able to match any positive and negative integer number that has +
or -
sign optional.
Read more about Character Classes and test your regex pattern online at Rubular
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 |