简体   繁体   English

使用 gsub 和块替换模式

[英]Replace patterns using gsub and blocks

When you have strings like this:当你有这样的字符串时:

str = "john_1"
str = "joe_34"
str = "jack_54"

And you want to get all characters preceding the underscore, you can use split:并且您想要获取下划线之前的所有字符,您可以使用 split:

str.split("_")[0]
 => "john" 

You can also achieve the same using matched groups and \\1 \\2 matchers:您还可以使用匹配组和 \\1 \\2 匹配器来实现相同的效果:

str = "john_1"
str.gsub(/(.+)_.+/,'\1')

I am trying to understand what happens in the block better.我试图更好地了解区块中发生的事情。 From what I read, it replaces the matched group with the result of executing the block.从我读到的,它用执行块的结果替换了匹配的组。 But I don't see it behaving that way.但我不认为它会那样做。

In the below example, if it is supposed to replace the matched group with the result of the block:在下面的示例中,如果应该用块的结果替换匹配的组:

str = "john_1"
str.gsub(/.+(_.+)/) {|m| ""}
 => "" 

Then why is the return value an empty string?那为什么返回值是空字符串呢? The result should be "john" because only the underscore and all subsequent characters were matched and replaced with an empty string.结果应该是“john”,因为只有下划线和所有后续字符匹配并替换为空字符串。 What am I missing?我错过了什么?

gsub replaces all occurrences of input pattern with the value of the result of the block. gsub用块的结果值替换所有出现的输入模式。 Here /.+(_.+)/ will match the whole string john_1 , hence the whole string will be replaced with ""这里/.+(_.+)/将匹配整个字符串john_1 ,因此整个字符串将被替换为""

If you want to only match _1 part with regex, here is one way to do it with regex lookahead如果您只想将_1部分与正则表达式匹配,这是使用正则表达式先行的一种方法

str.gsub(/(?=.+)(_.+)/, "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM