22

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?

3
  • what output you are expecting?
    – SMA
    Commented Oct 23, 2014 at 13:12
  • 1
    I'm expecting a match actually. The text between the second last _ and the end of the string should be matched. Commented Oct 23, 2014 at 13:14
  • 4
    @ЗахарJoe so "A nice text is a pleasure for your body and soul"? be explicit
    – caub
    Commented Jan 4, 2019 at 10:37

2 Answers 2

24

The text between the second last _ and the end of the string should be matched

Use a negated character class, like

([^.]*$)

It will match everything from the end of the string that isn't ., resulting in the last quote (assuming each quote ends with a .)

http://regex101.com/r/fA3pI7/1

4
  • Well, that doesn't quite work in my case. Sometimes the last _ _ block contains multiple sentences separated by dots. I'm explicitly looking to match the second last _ (underscore) Also sometimes the string itself ends with a dot. Commented Oct 23, 2014 at 13:51
  • Are you using any programming language to process the result, or do you need the answer just using a regular expression?
    – ʰᵈˑ
    Commented Oct 23, 2014 at 13:58
  • I'm using a TextMate text editor to process those lines, so it should be just an expression. Commented Oct 23, 2014 at 14:03
  • 3
    ʰᵈˑ : your solution was excellent: clean and elegant. Thanks. It may not have worked for the OP but for a reverse search is very useful for many applications. e.g extracting the filename from a full path we can use "[^/]*$" Commented Jun 25, 2019 at 7:33
11

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.

3
  • M, hey, getting somewhere! It works but there's a catch/bug: it matches until the next line with any underscore inside. I.e. if the lines following the intended match are without underscores then the result will expand until the very first line that contains them. But that line won't be included, only the ones preceding it will. Example: 1: this is a line with matches and stuff 2: this is just a simple line 3: another line 4: now we have matches again. The expression will match everything from 1: _stuff until the 3: line Commented Oct 23, 2014 at 14:19
  • @ЗахарJoe: See my edit, I've added line break. Is this working?
    – Toto
    Commented Oct 23, 2014 at 14:24
  • M42, working perfectly! However it's not the end, I'd really be grateful if you could explain how that was achieved, could be useful to other folks with a similar problem. The expression is quite advanced. Commented Oct 23, 2014 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.