I just want to know whether ruby regex has a not match operator just like !~
in perl. I feel it's inconvenient to use (?!xxx)
or (?<!xxxx)
because you cannot use regex patterns in the xxx
part.
A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing.
=~ is Ruby's pattern-matching operator. It matches a regular expression on the left to a string on the right. If a match is found, the index of first match in string is returned. If the string cannot be found, nil will be returned.
Ruby | Regexp match() function Regexp#match() : force_encoding?() is a Regexp class method which matches the regular expression with the string and specifies the position in the string to begin the search. Return: regular expression with the string after matching it.
Operators used in regular expressions include: Union: If R1 and R2 are regular expressions, then R1 | R2 (also written as R1 U R2 or R1 + R2) is also a regular expression. L(R1|R2) = L(R1) U L(R2). Concatenation: If R1 and R2 are regular expressions, then R1R2 (also written as R1.
Yes: !~
works just fine – you probably thought it wouldn’t because it’s missing from the documentation page of Regexp
. Nevertheless, it works:
irb(main):001:0> 'x' !~ /x/ => false irb(main):002:0> 'x' !~ /y/ => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With