简体   繁体   English

ruby正则表达式匹配字符串的结尾

[英]ruby regex match from end of string

I've read somewhere today that regex tag in SO gets most "give me ze code" type questions, so I was cautious in asking... I tried, but if this is a duplicate please let me know so I can delete. 我今天在某地读过,SO中的正则表达式标签最能“给我ze代码”类型的问题,所以我谨慎地问...我试过了,但如果这是重复的,请告诉我,以便我可以删除。

[First]sometext[Second]

I would like to use Regex in Ruby to return value between second []: 我想在Ruby中使用Regex在second []之间返回值:

Second

I so far have: 我到目前为止:

(?<=\[)(.*)(?=\])

which returns 返回

First]sometext[Second

\[.*?(\[)

this grouping will return 这个分组将返回

[First]sometext[

so I've been struggling to somehow mix the two but no luck.. hope someone can help. 所以我一直在努力以某种方式混合这两个但没有运气..希望有人可以提供帮助。

The closest reference I can find in SO was searched with "match second or nth occurence in regex" which I couldn't get it to work on my issue. 我在SO中找到的最接近的参考文献是在“正则表达式中匹配第二或第n次出现”中搜索的,我无法在我的问题上使用它。

my workaround was to use gsub to replace the [First] with "" to the initial string with: 我的解决方法是使用gsub将[First]和“”替换为初始字符串:

\[(.*?)\]

and then do another match.. but I would like know how it can be done with on regex usage. 然后做另一场比赛..但我想知道如何使用正则表达式使用。

> s = "ipsum[First]sometext[Second]lorem"
=> "ipsum[First]sometext[Second]lorem"
> s =~ /\[.*?\].*?\[(.*?)\]/
=> 5
> $1
=> "Second"

Why not use a greedy search at the beginning .* so capture as much as possible? 为什么不在开头使用贪婪的搜索.*尽可能多地捕获?

^.*\[(.*?)\]

Demo 演示

You could then make it un-greedy (to capture only the stuff in the first [...] block) by appending ? 然后你可以通过附加来使它不贪婪(仅捕获第一个[...]块中的东西) ? as ^.*? 作为^.*? .

There's a lot of ways to handle this. 有很多方法可以解决这个问题。 One that hasn't been mentioned yet is the end of input anchor $ . 尚未提及的是输入锚点$的结束。

s = "[First]sometext[Second]"
s.match(/[\[][^\[]+[\]]$/)  # => #<MatchData "[Second]">
s.match(/[\[]([^\[]+)[\]]$/)  # => #<MatchData "[Second]" 1:"Second">

This only works in the specific case where [something] ends your string. 这仅适用于[something]结束字符串的特定情况。

The section on 'anchors' describes the subtle differences between $ and \\z and \\Z . 'anchors'部分描述了$\\z\\Z之间的细微差别。

http://www.ruby-doc.org/core-2.1.2/Regexp.html http://www.ruby-doc.org/core-2.1.2/Regexp.html

You were close. 你很亲密 Use this to capture the relevant section in the first capturing group, then access it with $1 使用此选项捕获第一个捕获组中的相关部分,然后使用$1访问它

.*?\\[.*?\\[(.*?)\\].*

Here's a way that may be more convenient if you just want the text enclosed by the last [..] in a string that may contain any number of [..] 's. 如果你只想要一个包含任意数量的[..]的字符串中的最后一个[..]所包含的文本,这可能会更方便。 [Edit: I'll leave this, but @OnlineCop has a better solution for the objective I described in the previous sentence. [编辑:我会离开这个,但@OnlineCop有一个更好的解决方案,我在上一句中描述的目标。 It's also an excellent answer to the original question. 这也是原始问题的绝佳答案。 ] ]

Reverse the string, then search for a substring with a regex that captures ] in the non-capture group (?:\\]) , captures *.? 反转字符串,然后在非捕获组(?:\\])搜索带有捕获]的正则表达式的子字符串,捕获*.? in capture group 1 (the ? making it non-greedy) and captures [ in a second non-capture group. 在捕获组1( ?使它非贪婪)和捕获[第二非捕获组中使用。 Lastly, retrieve and reverse the string contained in capture group 1: 最后,检索并反转捕获组1中包含的字符串:

str = "The [first] time I was mad.  The [second] time I was irate!"
str.reverse[/(?:\])(.*?)(?:\[)/,1].reverse #=> "second"

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

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