正则表达式从字符串末尾开始搜索(反向) [英] Regex start searching from the end of the string (reverse)
问题描述
我有一些字符串,其中的块用下划线括起来.示例:
*Text* _word_ 这是我们每天阅读的内容._文本中的文字_可以非常具有表现力._一篇好文是你身心的愉悦_(奥斯卡·王尔德)
在上面的示例中,有三个这样的块,但数量因字符串而异.我只想匹配最后一个,即从行尾开始懒洋洋地跳过字符直到找到第一个 _ ,跳过任何后续字符直到遇到第二个 _ 并在那里停止.>
如果我们要查找字符串中的第一个块,很容易找到类似的块,但是如何找到 最后一个 块呢?
试试看:
((?:_[^_\r\n]*){2})$
它匹配一个下划线,后跟任意数量的任何非下划线或换行符的字符,所有这些都在留置结束之前出现两次.
I have strings that have blocks enclosed in underscores in them. Example:
*Text* _word_ it is something we read every day. _Words in texts_ can be really expressive. _A nice text is a pleasure for your body and soul_ (Oscar Wilde)
In the example above there are three such blocks but the number varies from string to string. I want to match only the last one, i.e. starting from the end of the line lazily skip characters until the first _ is found, skip any following characters until encountering the second _ and stop right there.
It is easy to to find a similar block if we were looking for the very first one inside the string, but how about finding the last one?
Have a try with:
((?:_[^_\r\n]*){2})$
It matches an underscore followed by any number of any character that is not underscore or line break, all that occurs twice before the end of lien.
这篇关于正则表达式从字符串末尾开始搜索(反向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!