ruby中的正则表达式_Ruby中的正则表达式

ruby中的正则表达式

Regular expressions are basically used to work with character/text patterns in ruby.

正则表达式基本上用于在ruby中处理字符/文本模式。

Regular Expressions  in Ruby

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

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

Things like these can be done in Regular expressions.

这些事情可以在正则表达式中完成。

Ruby:正则表达式 (Ruby: Regular Expressions)

Regular Expressions in Ruby

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

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

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中的正则表达式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值