ruby中的正则表达式
Ruby正则表达式 (Ruby regular expressions)
Regular expressions are a series of characters which are used to define the pattern to carry out a specific search. It can also be termed as a kind of language which tells the text pattern. It is also known by regexp. Forward slashes are used for writing the pattern.
正则表达式是一系列字符,用于定义模式以执行特定搜索。 它也可以被称为一种能说明文本模式的语言。 regexp也知道它。 正斜杠用于写入模式。
The basic syntax of regex looks like:
正则表达式的基本语法如下:
/search string/
To conduct pattern matching, you will have to use ~= or #match operator. Now let us understand each of the methods broadly.
要进行模式匹配,您将必须使用〜=或#match运算符。 现在让我们大致了解每种方法。
〜=运算符 (~= operator)
It is a basic pattern matching operator which consumes two operands namely pattern or regular expression and a string. No doubt, the regexp is matched with the string. The operator returns integer value if the pattern is matched otherwise nil. That integer value is nothing but the index of the first match. Let us understand the working of ~= operator with the help of a supporting example:
它是一个基本的模式匹配运算符,它消耗两个操作数,即模式或正则表达式以及一个字符串。 毫无疑问,正则表达式与字符串匹配。 如果模式匹配,则运算符返回整数值,否则返回nil。 该整数值不过是第一个匹配项的索引。 让我们借助一个支持示例来了解〜=运算符的工作方式:
Example:
例:
=begin
Ruby program to demonstrate use of ~= operator.
=end
str1 = "Dreams are not those which we see in night but they are those which do not let us sleep.";
str2 = "Little failures makes you one step closer to success.";
if ( str1 =~ /Dreams/ )
puts "First string is related to Dreams"
end
if ( str2 =~ /failures/ )
puts "Second string is related to success"
end
Output
输出量
First string is related to Dreams
Second string is related to success
If you have gone through the working of .include? method then you can relate the similarity between the working of both.
如果您已完成.include的工作? 方法,然后可以关联两者的工作之间的相似性。
#match运算子 (#match operator)
The match operator matches the regular expression or pattern with the specified string and returns an instance of class MatchData if the pattern is matched otherwise returns nil.
match运算符将正则表达式或模式与指定的字符串匹配,如果模式匹配则返回MatchData类的实例,否则返回nil。
Let us go through its implementation with the help of a Ruby code.
让我们在Ruby代码的帮助下完成其实现。
=begin
Ruby program to demonstrate use of match operator.
=end
str1 = "Dreams are not those which we see in night but they are those which do not let us sleep."
str2 = "Little failures makes you one step closer to success."
puts /Dreams/.match(str1)
puts /failures/.match(str2)
Output
输出量
Dreams
failures
You must be thinking that it is returning string but in actual, it is returning an object of class MatchData.
您必须考虑到它正在返回字符串,但实际上,它正在返回MatchData类的对象。
Now, let us get some knowledge about Regular Expression Modifies which are mentioned below with their description in brief.
现在,让我们获得一些有关正则表达式修改的知识,下面将简要介绍它们。
i – i modifier ignores case (uppercase or lowercase) while matching the text.
i – i修饰符在匹配文本时忽略大小写(大写或小写)。
o – It is used to perform #{} interpolations but only one time when the first time literal pattern is checked.
o –用于执行#{}插值,但仅在第一次检查文字模式时执行一次。
x – It permits comments to be included in the regexp. It also ignores white spaces during evaluation.
x –允许将注释包含在正则表达式中。 在评估期间,它也忽略空格。
m – It matches multiple lines and understands the newline as a normal character.
m –匹配多行并将换行符理解为正常字符。
翻译自: https://www.includehelp.com/ruby/regular-expressions.aspx
ruby中的正则表达式