ruby中的正则表达式
Regular expressions are basically used to work with character/text patterns in ruby.
正则表达式基本上用于在ruby中处理字符/文本模式。
![Regular Expressions in Ruby](https://i-blog.csdnimg.cn/blog_migrate/7ef7a5f16bb45b418782e21754443012.png)
sub()
method isn't a regular expression. But it explains when to use the regular expressions. It replaces part of string with other string specified by the user. It contains two parameters, the first parameter is the substring
which is to be replaced and second parameter is the substring
that you should replace with.
sub()
方法不是正则表达式。 但是它说明了何时使用正则表达式。 它用用户指定的其他字符串替换字符串的一部分。 它包含两个参数,第一个参数是substring
将被替换 ,第二个参数是substring
,你应该将其替换 。
For example, to replace the substring bad in badjohnny with good we can use:
例如,在具有良好的 ,我们可以使用badjohnny更换串坏 :
puts "badjohnny".sub('bad','good')
It replaces bad
with good
. But it doesn't replace all the instances of the substring.
用good
代替bad
。 但是,它不会替换子字符串的所有实例。
![Regular Expressions in Ruby](https://i-blog.csdnimg.cn/blog_migrate/2d1e0381c9a0e97ca01125b222ea5105.png)
You could see that it replaces only the first instance of the substring bad. To replace all the instances of substring gsub()
method is used.
您会看到它仅替换了子字符串bad的第一个实例。 要替换子字符串的所有实例,请使用gsub()
方法。
![Regular Expressions in Ruby](https://i-blog.csdnimg.cn/blog_migrate/4656fca841eb0817aa7334e74b7e6431.png)
Things like these can be done in Regular expressions.
这些事情可以在正则表达式中完成。
Ruby:正则表达式 (Ruby: Regular Expressions)
![Regular Expressions in Ruby](https://i-blog.csdnimg.cn/blog_migrate/3601188f2724a6f8c3ee7b98cf394c52.png)
Variable a
has the value "Hey, what do you want?". If you want to replace the "Hey" with "Excuse me" then you can use regular expressions. As already said, sub()
method have two parameters. We have to replace the first three characters with our desired string.
变量a
的值为“嘿,您想要什么?” 。 如果要将“嘿”替换为“ 对不起”,则可以使用正则表达式。 如前所述, sub()
方法具有两个参数。 我们必须用所需的字符串替换前三个字符。
The first parameter contains /^.../
第一个参数包含/^.../
Regular expression starts and ends with forward slash /
. ^
symbol is used to match the substring from the beginning of the string. It didn't replace the characters in the middle with, it replaced the characters from the beginning. Dots .
represents any characters, it may be numbers, alphabets, symbols anything
正则表达式以正斜杠/
开头和结尾。 ^
符号用于从字符串开头匹配子字符串。 它没有替换中间的字符,而是从一开始就替换了字符。 点 .
代表任何字符,可以是数字,字母,符号等
Now, what it does is, it matches 3 characters from the beginning in our case it is "Hey" and replaces it with the string specified by the user.
现在,它的作用是,从本例的开头开始匹配3个字符,即为“ Hey”,并将其替换为用户指定的字符串。
![Regular Expressions in Ruby](https://i-blog.csdnimg.cn/blog_migrate/a17ff6877f4a931e807fbb6e364acb0e.png)
Ruby:扫描方法 (Ruby: Scan method)
Variable a contains some value. Using scan()
method, we are going to scan the variable a and using regular expression, we read the characters and display it in the way we want.
变量a包含一些值。 使用scan()
方法,我们将扫描变量a并使用正则表达式,读取字符并以所需方式显示它。
The regular expression (/.../)
reads three characters and stores it temporarily in variable x, using that value we can do our desired operation. As said before, dots represent any character so it reads any three characters (including spaces). We've just printed the value of x.
正则表达式(/.../)
读取三个字符并将其临时存储在变量x中 ,使用该值我们可以执行所需的操作。 如前所述,点代表任何字符,因此它可以读取任何三个字符(包括空格)。 我们刚刚打印了x的值。
![Scan method in Ruby](https://i-blog.csdnimg.cn/blog_migrate/a6ab8736fc7a072e280a15d5e58940b0.png)
Here inside regular expressions we have used '\S'
. \S
is a non-white space character. It can be a number or any letter. We have given three \S\S\S, it iterates through the variable and finds three non-white space characters and stores in the variable x.
在正则表达式中,我们使用了'\S'
。 \S
是非空格字符。 它可以是数字或任何字母 。 我们给了三个\ S \ S \ S ,它遍历变量并找到三个非空格字符并将其存储在变量x中 。
First it reads "Rub"
stores in x and prints it. Then it reads "y"
that is y and a space it ignores it because it contains white space characters. Likewise, "a"
and "l"
in typical is ignored.
首先,它读取x中的 "Rub"
存储并进行打印。 然后,它读取为y的 "y"
,并忽略一个空格 ,因为它包含空格字符。 同样,通常忽略"a"
和"l"
。
翻译自: https://www.studytonight.com/ruby/regular-expressions-in-ruby
ruby中的正则表达式