Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby regular expression have a not match operator like "!~" in Perl?

Tags:

regex

ruby

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.

like image 850
user1793091 Avatar asked Dec 07 '12 07:12

user1793091


People also ask

What kind of regex does Ruby use?

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.

What does =~ mean in Ruby regex?

=~ 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.

How do you use the Match function in Ruby?

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.

Which operator is used in regular expression?

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.


1 Answers

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 
like image 56
Konrad Rudolph Avatar answered Sep 27 '22 21:09

Konrad Rudolph